不同的警报视图和视图控制器

发布于 2024-10-28 20:16:13 字数 229 浏览 6 评论 0原文

在同一个视图控制器中,我必须显示不同的警报以及由警报按钮触发的不同操作(此视图控制器是警报的委托)。

有没有办法重用警报代码 init/show/release。

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

考虑到我需要区分警报,

In a same view controller, I have to show different alerts with different actions triggered by the alert buttons (this view controller is the delegate of the alerts).

Is there a way to reuse the alert code init/show/release, considering that in

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

I need the alerts to be distinguishable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤单情人 2024-11-04 20:16:14

正是 7KV7 所说的。您需要标记警报视图,并在事件处理程序中检查发件人的标记是什么。这样,您可以在同一事件处理程序 (clickedButtonAtIndex) 中为不同的警报视图创建不同的操作。

Exactly what was stated by 7KV7. You need to tag the alert view, and check in your event handler what the sender's tag was. This way, you can create different actions for different alert views in the same eventhandler (clickedButtonAtIndex).

︶ ̄淡然 2024-11-04 20:16:13

您可以定义一组常量来表示您正在管理的每种不同类型的警报视图。例如:

enum {
    MyFirstTypeOfWarning,
    MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;

然后,每当您发现自己需要呈现 UIAlertView 时,调用一个包装 init/show show 代码并设置 UIAlertView 标签的方法:

- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
    // Standard alloc/init stuff
    [alertView setTag:tag];
    [alertView show];
 }

然后,在alertView:clickedButtonAtIndex: 中,您可以检查警报的标签查看传入的内容并做出相应的响应。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == MyFirstTypeOfWarning) {
        // Process button index for first type of alert.
    } ...
}

You can define a set of constants to represent each different type of alert view you're managing. For instance:

enum {
    MyFirstTypeOfWarning,
    MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;

Then, whenever you find yourself needing to present a UIAlertView, call a method that wraps up the init/show show code and sets the tag of the UIAlertView:

- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
    // Standard alloc/init stuff
    [alertView setTag:tag];
    [alertView show];
 }

Then, in alertView:clickedButtonAtIndex: you can check the tag of the alert view passed in and respond accordingly.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == MyFirstTypeOfWarning) {
        // Process button index for first type of alert.
    } ...
}
如若梦似彩虹 2024-11-04 20:16:13

您可以在此处获取警报视图

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
     if([alertView isEqualTo:yourAlertView]) // you can try isEqual:
    {
    //Do something
    }
//Another option is set tags to alertviews and check these tags
// if(alertView.tag == yourAlertView.tag)
//{
//Do something
//}
    }

You can get the alert view here itself

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
     if([alertView isEqualTo:yourAlertView]) // you can try isEqual:
    {
    //Do something
    }
//Another option is set tags to alertviews and check these tags
// if(alertView.tag == yourAlertView.tag)
//{
//Do something
//}
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文