Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISwitch: Swift 3: Programmatically

Tags:

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.

like image 890
Robert Smith Avatar asked Sep 28 '16 23:09

Robert Smith


People also ask

How do you set UISwitch state programmatically?

If you are using a UISwitch, then as seen in the developer API, the task setOn: animated: should do the trick. Show activity on this post. UISwitches have a property called "on" that should be set.

How do I use the switch button in Xcode?

Go to the Storyboard and drag a Button, a Label and a Switch to the main view. Give the button a title of "Change State" and the Text Field a text of "The Switch is On". The Storyboard will look like this.


2 Answers

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

like image 96
crashoverride777 Avatar answered Sep 16 '22 21:09

crashoverride777


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){
        print("on")
    }
    else{
        print("off")
    }
}
like image 32
DaniChi Avatar answered Sep 16 '22 21:09

DaniChi