Search code examples
iphoneobjective-ciosxcodeuiactionsheet

UIActionSheet, cancel button runs method


Every time I press a cancel button in UIActionSheet, it runs a method. I have no idea why, I checked whole my code many times, but I still can't see the problem. Could you help me to find it ?

-(IBAction)moreOptions
{

    giftTitle = self.title;

     if(![giftTitle isEqualToString:@"bla"])
     {
        actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
                                                            delegate:self
                                                   cancelButtonTitle:@"Back"
                                              destructiveButtonTitle:nil
                                                   otherButtonTitles:@"Send via email",
                                  @"Read in Wikipedia"
                                  , nil];
     }
    else 
    {
        actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
                                                 delegate:self
                                        cancelButtonTitle:@"Back"
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:@"Send via email",
                       @"Read in Wikipedia", @"Pineapple mode"
                       , nil];

    }
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view.window];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    // выстраеваем дальнейшие действия кнопок

        switch (buttonIndex) 
        {
            case 0:
                [self showPicker];
            break;

            case 1:
                [self goWiki];
            break;

            case 2:
                [self showPineapple];
            break;

            default:
            break;

        }

}

So it runs method showPineapple. Please help !


Solution

  • You need to implement something like this:

    Change your if and else sections to add a unique tag for each UIActionSheet:

    if(![giftTitle isEqualToString:@"bla"]) {
        actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
                                                 delegate:self
                                        cancelButtonTitle:@"Back"
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:@"Send via email", @"Read in Wikipedia" , nil];
        actionSheet.tag = 10;
    } else {
        actionSheet = [[UIActionSheet alloc]initWithTitle:giftTitle
                                                 delegate:self
                                        cancelButtonTitle:@"Back"
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:@"Send via email", @"Read in Wikipedia", @"Pineapple mode", nil];
    
        actionSheet.tag = 20;
    }
    

    Then look for the tag in the actionSheet:clickedButtonAtIndex: message handler:

    case 2:
       if (actionSheet.tag == 20)
          [self showPineapple];
       break;
    

    This means [self showPineapple] will only run in the else scenario, while nothing will happen in the if scenario (just as nothing will happen for buttonIndex 3 in the else scenario (where the Cancel button is indeed at index 3) .