0

每次我在 UIActionSheet 中按下取消按钮时,它都会运行一个方法。我不知道为什么,我检查了整个代码很多次,但我仍然看不到问题。你能帮我找到吗?

-(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;

        }

}

所以它运行方法showPineapple。请帮忙 !

4

2 回答 2

1

是的,当您按下操作表上的取消按钮时,它的委托函数总是调用,最后一个索引。

如果您正在实施多个操作表,那么只需按标签值使用它。

于 2012-08-31T15:25:46.083 回答
0

你需要实现这样的东西:

更改您的ifelse部分,为每个 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;
}

然后在 actionSheet:clickedButtonAtIndex: 消息处理程序中查找标记:

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) .

于 2012-08-31T15:46:53.397 回答