代表的几个UIAlertViews

时间:2010-02-26 00:00:11

标签: ios iphone delegates uialertview uialertviewdelegate

目前我已经有一个班级在这里和那里弹出UIAlertView。目前,同一个类是这些的委托(它是非常合乎逻辑的)。不幸的是,这些UIAlertView将调用该类的相同委托方法。现在,问题是 - 您如何知道调用委托方法的警报视图?我只想检查警报视图的标题,但这不是那么优雅。什么是处理多个UIAlertView的最优雅方式?

5 个答案:

答案 0 :(得分:102)

UIAlertView标记为:

#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) {
        // ...
    }
}

答案 1 :(得分:4)

仅供参考,如果您只想定位iOS 4用户(这是合理的now that ~98.5% of clients have at least iOS 4 installed),您应该能够使用Blocks来对UIAlertViews进行非常好的内联处理。

这是一个解释它的Stackoverflow问题:
Block for UIAlertViewDelegate

我尝试使用Zachary Waldowski的BlocksKit框架。他的UIAlertView(BlocksKit) API参考看起来非常好。但是,我尝试按照his instructions to import the BlocksKit framework进入我的项目,但不幸的是我无法让它工作。

所以,正如CanBerkGüder所说,我现在使用了UIAlertView标签。但是在将来的某个时候,我将尝试使用Block(最好是开箱即用的ARC)!

答案 2 :(得分:3)

更容易&较新的

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

全部完成!

答案 3 :(得分:1)

您可以通过增强UIAlertView来使用块回调来克服整个考验,并防止自己使用标签。看看this blog post我写过这个主题。

答案 4 :(得分:0)

我一直以为使用标签有点像黑客。如果你使用它们,至少为标签号设置一些定义的常量。

相反,我使用这样的属性:

在界面部分:

<environment names="Development">
    <script src="~/lib/jquery-validation/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>

创建警报视图:

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

委托方法:

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