【问题标题】:Multiple UIAlertViews in the same view同一个视图中的多个 UIAlertViews
【发布时间】:2012-10-04 16:15:57
【问题描述】:


我有两个带有确定/取消按钮的 UIAlertView。
我通过以下方式捕获用户响应:

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

我遇到的问题是,当前打开了哪个 alertView?
在每一个上单击确定/取消时,我有不同的操作要做...

【问题讨论】:

标签: objective-c ios uialertview


【解决方案1】:

您有多种选择:

  • 使用 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.
        }
    }
    
  • 使用UIViewtag 属性:

    #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).
    

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (alertView.userInfo == firstUserInfo) {
            // do something.
        } else if (alertView.userInfo == secondUserInfo) {
            // do something else.
        }
    }
    

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多