I am using Swift 4. I have a segmented control on the right of a top UINavigationBar
like so:
When I create an outlet from the Storyboard to my code, by default it wants it to be a UIBarButtonItem
, but for my action I cannot access selectedSegmentIndex
without making it a UISegmentedControl
.
So I make my outlet like this:
@IBOutlet weak var saveAsControl: UISegmentedControl!
My action, as a test, is this:
@IBAction func saveAsTypeAction(_ sender: Any) {
print("tapped")
switch saveAsControl.selectedSegmentIndex
{
case 0:
print("video selected")
case 1:
print("gif selected")
default:
break;
}
}
If I tap the segmented control, the debugger crashes and has the message:
-[UIBarButtonItem selectedSegmentIndex]
: unrecognized selector sent to instance
So I have no idea how to get the segmented control working. Seems like it has something to do with the UINavigationBar
, but I have no idea why it would be a problem.
You likely wired up your outlets wrong. Here is the correct setup:
Note that both the outlet and the action are wired to the segmented control and not the bar button itself.