when clicking the button action down below, I get an exception:
-[UIRoundedRectButton selectedSegmentIndex]: unrecognized selector sent to instance 0x8178b90 ;
‘
(it is also initialized as – (IBAction)genderBtn:(id)sender; in the header file).
I do not know if I should somehow initialize this into another method or not or initialized globally. Any method ideas would be much appreciated.
- (IBAction)submitButton:(id)sender {
double BAC=0;
// NSString *weight=weightTextField.text;
//Other variables etc.
UISegmentedControl *gender = (UISegmentedControl *)sender;
UIButton *gender = (UIButton *)sender;
if (gender.selected == 0 ) {
} else if (gender.selected = 1){
}
UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Your Results:"
message:[NSString stringWithFormat:@" Your Percentage is: "]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertMessage show];
}
The error is saying that although you think the sender value is a UISegmentedControl, it isn’t. It’s a UIRoundedRectButton. The result is that you end up sending a message to which only a UISegmentedControl implements to the UIRoundedRectButton, thus it does not recognize the selector. Make sure this action is connected to the right type of button.
EDIT: Alright. I looked at your code from before. I thought the problem was that you used a normal UIButton instead of a UISegmentedControl, but the problem seems to really be that you shouldn’t be using the sender argument at all.
I take it that you have one UISegmentedControl for the user to select something, and one UIButton for them to tap when they’re done with their selection. The problem was that you were asking the sender argument (which was the submit button) what the selection state of the UISegmentedControl was. You need to store the UISegmentedControl in a property and use that in your submit method to get the selectedSegmentIndex.
The submit button calls this when it is pressed and gets the selected index from the segmented control which you store in a property.
Hook this IBOutlet to your segmented control and use that.