Search code examples
iosiphonexcodeuiactionsheet

iOS: ActionSheetStringPicker successAction: wrong selection


I am using the ActionSheetStringPicker from the ActionSheetPickers3.0 (since I no longer can use the old picker views because of iOS 8). This is how I am calling the method:

ActionSheetStringPicker *genderPicker = [[ActionSheetStringPicker alloc] 
initWithTitle:@"Gender" rows:categoryTypes initialSelection:nil target:self 
successAction:@selector(selectedGender:) cancelAction:@selector(cancelledGender) 
origin:gender];

[genderPicker showActionSheetPicker];

My success action code is this:

-(void) selectedGender:(NSNumber *)genderSelected {

    NSLog(@"selected gender: %@",genderSelected);

    if (genderSelected == 0) {
        NSLog(@"male");
        gender.text = @"Male";
    } else {
        NSLog(@"female");
        gender.text = @"Female";
    }
}

I am getting either a 1 or 0 value from the selected gender (male/female) when I NSLog the genderSelected value. My problem is that in my if-statement it always says I picked female. I'm getting the correct value that I select from the picker, but can't seem to figure out what the problem is. Does anyone know how I can get my if-statement working? Thanks!


Solution

  • In your success callback method the parameter you are getting is of type NSNumber. You cannot directly compare the object of type NSNumber with int value 0. Therefore use the following :

    if (genderSelected.intValue == 0) {
        NSLog(@"male");
        gender.text = @"Male";
    } else {
        NSLog(@"female");
        gender.text = @"Female";
    }