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.
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.
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.
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
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")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With