同一视图中的多个UIAlertView

时间:2012-10-04 16:15:58

标签: objective-c ios uialertview


我有两个带有ok / cancel按钮的UIAlertViews。
我通过以下方式收到用户的回复:

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

我遇到的问题是,哪个alertView目前正在打开?
在每个单击ok / cancel时我有不同的动作......

2 个答案:

答案 0 :(得分:20)

您有几种选择:

  • 使用ivars。创建警报视图时:

    myFirstAlertView = [[UIAlertView alloc] initWith...];
    [myFirstAlertView show];
    // similarly for the other alert view(s).
    

    在委托方法中:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (alertView == myFirstAlertView) {
            // do something.
        } else if (alertView == mySecondAlertView) {
            // do something else.
        }
    }
    
  • 使用tag的{​​{1}}属性:

    UIView

    #define kFirstAlertViewTag 1
    #define kSecondAlertViewTag 2
    

    UIAlertView *firstAlertView = [[UIAlertView alloc] initWith...];
    firstAlertView.tag = kFirstAlertViewTag;
    [firstAlertView show];
    // similarly for the other alert view(s).
    
  • 子类- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { switch (alertView.tag) { case kFirstAlertViewTag: // do something; break; case kSecondAlertViewTag: // do something else break; } } 并添加UIAlertView属性。这样,您就可以在警报视图中添加标识符。

    userInfo

    @interface MyAlertView : UIAlertView
    @property (nonatomic) id userInfo;
    @end
    

    myFirstAlertView = [[MyAlertView alloc] initWith...];
    myFirstAlertView.userInfo = firstUserInfo;
    [myFirstAlertView show];
    // similarly for the other alert view(s).
    

答案 1 :(得分:1)

UIAlertViewUIView子类,因此您可以使用其tag属性进行标识。因此,当您创建警报视图时,设置其标记值,然后您将能够执行以下操作:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
   if (alertView.tag == kFirstAlertTag){
      // First alert
   }
   if (alertView.tag == kSecondAlertTag){
      // First alert
   }
}
相关问题