2012-08-31 65 views
0

每當我按UIActionSheet中的取消按鈕,它都會運行一個方法。我不知道爲什麼,我多次查看了整個代碼,但仍然看不到問題。你能幫我找到它嗎?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。請幫忙 !

回答

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; 

這意味着[self showPineapple]只會在運行3210場景,而if場景中什麼都不會發生(就像else場景(其中「取消」按鈕的確位於索引3處)中的buttonIndex 3一樣)。

+0

是的,它的工作!謝謝 ! 有趣的是,我從來沒有聽說過標籤屬性。 – SmartTree

1

雅當你按下操作表上的取消按鈕然後它的委託功能總是調用,最後一個索引。

如果您正在實施多個操作表,那麼只需通過標記值來使用它。

+0

謝謝! 所以我應該添加案件3什麼都沒有呢? – SmartTree

+0

你可以在破壞性的地方放置@「通過電子郵件發送」按鈕 – Rajneesh071

+0

破壞性按鈕索引爲0 – Rajneesh071