2010-01-30 71 views
1

嗨iam用2個按鈕創建UIActionSheet。現在我想要兩個按鈕做單獨的工作。 我怎麼可以聲明對我的2個按鈕:UIActionSheet with 2 buttons .... {iPhone SDK}

- (void)actionSheet:(UIActionSheet *)menu 
       didDismissWithButtonIndex:(NSInteger)buttonIndex 

我用這個代碼:

if (buttonIndex != [menu cancelButtonIndex]) { 
    // do somthing 
} 

但如果用戶單擊除取消按鈕做財產以後任何按鈕,這意味着什麼。 謝謝。

回答

1
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == [menu cancelButtonIndex]) { 
     // do something because the user clicked "cancel". 
    } else { 
     // do something because the user clicked "the other button". 
    } 
} 
+0

沒有任何變化! :)按鈕2呢? 哼? – Momi 2010-01-30 21:43:12

+0

@Momeks:所以你的意思是你有*三個按鈕:) – kennytm 2010-01-31 05:44:44

+0

親愛的。我有3個按鈕,例如 按鈕1/2/3 /取消。 現在我能做什麼>? – Momi 2010-01-31 08:47:49

4

這將工作得更加普遍。你可以把它擴展到儘可能多的按鈕,只要你喜歡:

- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    switch (buttonIndex) { 
     case 0: 
      //do something 
      break; 
     case 1: 
      //do something else 
      break; 
     default: 
      break; 
    } 
} 
+0

謝謝你的工作很棒! – Momi 2010-01-30 21:19:50

+0

但有問題!例如,如果把一個alertview的情況下0我得到一些錯誤!爲什麼? http://freezpic.com/pics/5a582757ae023e8f55dfcbf535fdbaa3.jpg – Momi 2010-01-30 21:32:30

+0

在聲明UIAlertView之前你有錯誤。檢查上面的行,它可能會丟失分號或類似的東西。 – 2010-01-30 22:06:00

0
- (void)actionSheet:(UIActionSheet *)menu didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    switch (buttonIndex) { 

     case 0: 

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" 
message:@"hooo" 
delegate:self 
cancelButtonTitle:@"boo" 
otherButtonTitles:@"yoo"]; 

      [alert show]; 
      [alert release]; 

      break; 
      case 1: 
      self.view.backgroundColor = [UIColor redColor]; 
       break; 

     default: 
      break; 
    } 
}