Cannot find 'segmentedControl' in scope?

I am trying to learn swift and have a problem with segmented controls.
I have an IBAction which will use switch to select 'Male' or 'Female'.
Here is the code:
Code Block
@IBAction func genderButton(_ sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex {
        case 0:
            userGender = "Female"
        case 1:
            userGender = "Male"
        default:
            return
        }
    }


However I get an error on the switch line:
Code Block
Cannot find 'segmentedControl' in scope

I have looked on the web and the method looks OK to me, so why do I get this error?


Answered by Claude31 in 670386022
Normally, you don't need the IBOutlet. With the code I proposed, it's enough to connect the IBAction.
Looks like you have not defined segmentedControl anywhere. In fact, you have to use sender (which is in fact the segmentedControl).

Just change as:
Code Block
@IBAction func genderButton(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
userGender = "Female"
case 1:
userGender = "Male"
default:
return
}
}


If that's OK, please don't forget to close the thread by marking the correct answer (it seems you have several other threads that you didn't close ; please do it as well)

Its me! I stupidly missed an IBOutlet for the segmented control.
Need new glasses!
Accepted Answer
Normally, you don't need the IBOutlet. With the code I proposed, it's enough to connect the IBAction.
Cannot find 'segmentedControl' in scope?
 
 
Q