目前我有一门课出现 UIAlertView到处都是。目前,同一个类是这些的委托(这是非常合乎逻辑的)。不幸的是,这些 UIAlertViews 将调用该类的相同委托方法。现在的问题是 - 您如何知道从哪个警报视图调用委托方法?我想只检查警报视图的标题,但这不太优雅。处理多个问题最优雅的方式是什么? UIAlertView是?

有帮助吗?

解决方案

标签的UIAlertViews这样的:

#define kAlertViewOne 1
#define kAlertViewTwo 2

UIAlertView *alertView1 = [[UIAlertView alloc] init...
alertView1.tag = kAlertViewOne;

UIAlertView *alertView2 = [[UIAlertView alloc] init...
alertView2.tag = kAlertViewTwo;

,然后将它们之间在使用这些标记委托方法区分:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == kAlertViewOne) {
        // ...
    } else if(alertView.tag == kAlertViewTwo) {
        // ...
    }
}

其他提示

仅供参考,如果您只想定位 iOS 4 用户(这是合理的) 现在 ~98.5% 的客户端至少安装了 iOS 4),您应该能够使用 Blocks 对 UIAlertViews 进行非常好的内联处理。

这是一个 Stackoverflow 问题来解释它:
UIAlertViewDelegate 的块

我尝试使用 Zachary Waldowski 的 BlocksKit 框架来实现此目的。他的 UIAlertView(BlocksKit) API 参考看起来真的很好。然而,我试图遵循 他导入 BlocksKit 框架的说明 到我的项目中,但不幸的是我无法让它工作。

因此,正如 Can Berk Güder 所建议的那样,我使用了 UIAlertView 暂时标记。但在未来的某个时候,我将尝试转向使用 Blocks(最好是开箱即用的支持 ARC 的块)!

更容易&更新

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 1) {
        // first alert...
    } else  {
        // sec alert...
    }
}

全部完成!

您可以克服这整个的考验,并防止自己从使用标签通过增强UIAlertView中使用块回调。请查看这个博客帖子我写上的对象。

我一直使用的标签思想是一个黑客攻击的一位。如果你使用它们,至少设置一些定义的常量的标签号。

相反,我使用性能是这样的:

在接口部分:

@property (nonatomic, weak) UIAlertView *overDueAlertView;
@property (nonatomic, weak) UIAlertView *retryPromptAlertView;

创建警报视图:

UIAlertView *alert = [[UIAlertView alloc] init...
self.overDueAlertView = alert;
[alert show];

委托方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (alertView == self.overDueAlertView) {
    // Overdue alert
  } else if (alertView == self.retryPromptAlertView) {
    // Retry alert
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top