Reputation: 584
I created a UISwitch on storyboard and tried to drag it to controller to create an action but there were only Insert Outlet / Insert Outlet Collection 2 options available? Why did this happen and how can I create action for UISwitch? I am using Xcode8.1
PS: The UISwitch was added on a container view
Upvotes: 0
Views: 149
Reputation: 31665
When "ctrl" + drag, you should see something like:
You should choose "Action" and let the event -which is by default- "Value Changed".
However, if you can't see this (and I assume you should, or there is somehow a problem), you can add an event programmatically to the switch outlet:
In viewDidLoad()
method, you need to add:
override func viewDidLoad() {
// ...
mySwitch.addTarget(self, action: #selector(mySwitchTapped), for: .valueChanged)
// ...
}
mySwitchTapped(mySwitch: UISwitch)
method:
func mySwitchTapped(mySwitch: UISwitch) {
if mySwitch.isOn {
} else {
}
}
Upvotes: 3