UISwitch: Swift 3: Programmatically

ⅰ亾dé卋堺 提交于 2019-12-06 17:18:38

问题


How can I programmatically add a uiswitch and call an action when on and one when off? Ive been searching for hours now. Can I please have some help? I know how to add the switch but it stays on the screen no matter what scene I'm on. So far, I've been able to add the button and make it switch from on to off, but for some reason the switch just says on the screen in every scene. I was lost after that so I followed this; from How to programmatically put a UISwitch in a SpriteKit/Skcene

Yes it is possible. Just use this code in your SKScene class:

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let switchDemo = UISwitch(frame:CGRectMake(150, 300, 0, 0))
    switchDemo.on = true
    switchDemo.setOn(true, animated: false)
    switchDemo.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged)
    self.view!.addSubview(switchDemo)
}

Helper method:

func switchValueDidChange(sender:UISwitch!)
{
    if (sender.on == true){
        print("on")
    }
    else{
        print("off")
    }
}

I kept getting errors so I did what Xcode suggested which ended up with the SIGBART error.


回答1:


You are calling the selector wrong on the addTarget action line. They finally changed it at one point in Swift 2 to get rid of using strings for selector method calls, which now makes them a lot less error prone.

Change it to this (Swift 3 syntax)

switchDemo.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)

You basically call #selector in the action parameter and include the method you want to call, in your case switchValueDidChange. Note the (_:) syntax at the end, thats indicating that the method you want to call takes a parameter, in your case a UISwitch.

 func switchValueDidChange(_ sender: UISwitch) {
     ...
 }

If you want to call a regular method that takes no parameters e.g

 func switchValueDidChange() {
 }

than you would just say

switchDemo.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)

without the (_:) syntax.

Hope this helps




回答2:


Updated to swift 5

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    let switchDemo = UISwitch(frame:CGRect(x: 150, y: 300, width: 0, height: 0))
    switchDemo.isOn = true
    switchDemo.setOn(true, animated: false)
    switchDemo.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
    self.view!.addSubview(switchDemo)
}

Helper method:

@objc func switchValueDidChange(_ sender: UISwitch!) {
    if (sender.isOn == true){
        print("on")
    }
    else{
        print("off")
    }
}



回答3:


With parameter:

@IBOutlet var categorySwitch:UISwitch!

var categorySwitchIsOn:Bool =  false

On viewDidLoad:

  override func viewDidLoad() {
        super.viewDidLoad()
        categorySwitch.addTarget(self, action:#selector(ViewController.categorySwitchValueChanged(_:)), for: .valueChanged)
    }

Associated function:

func categorySwitchValueChanged(_ sender : UISwitch!){

    if sender.isOn {
        categorySwitchIsOn =  true

    } else {

        categorySwitchIsOn =  false
    }


}


来源:https://stackoverflow.com/questions/39759253/uiswitch-swift-3-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!