2010-01-30 53 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; 
    } 
}