同一视图控制器中的两个警报视图

时间:2009-02-20 09:51:36

标签: objective-c iphone

我在同一个视图控制器中有两个UIAlertView,我想使用委托方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex

当按下警报视图中的按钮时,将调用此方法。但是,两个警报视图都会调用相同的方法。

如何区分两个警报视图?

1 个答案:

答案 0 :(得分:8)

显示警报时,将tag属性设置为不同的值。它只是一个整数,可以在回调/委托方法中查询。

这是一个例子(使用ActionSheet而不是AlertView,但原理完全相同):

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Some option", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheet.tag = 10;
[actionSheet showInView:self.view];
[actionSheet release];

然后在你的选择器中:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  switch (actionSheet.tag) {
    case 10:
      // do stuff
      break;
    case 20:
      // do other stuff
      break;
  }
}

当然,你使用常量而不是文字值,本地化字符串等,但这是基本的想法。

相关问题