同一视图中有多个UIAlertViews [英] Multiple UIAlertViews in the same view

查看:115
本文介绍了同一视图中有多个UIAlertViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个带有ok/cancel按钮的UIAlertViews.
我通过以下方式捕获用户的响应:


I have two UIAlertViews with ok/cancel buttons.
I catch the user response by:

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

我遇到的问题是,当前哪个警报视图处于打开状态?
在每个按钮上单击确定/取消"时,我有不同的操作要做.

The question I'm having is, which alertView is currently open?
I have different actions to do when clicking ok/cancel on each one...

推荐答案

您有几种选择:

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

  • Use ivars. When creating the alert view:

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.
    }
}

  • 使用 UIView tag 属性:

    #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 属性.这样,您可以在警报视图中添加标识符.

  • Subclass UIAlertView and add a userInfo property. This way you can add an identifier to your alert views.

    @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.
        }
    }
    

  • 这篇关于同一视图中有多个UIAlertViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆