Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 代表的多个视图_Ios_Iphone_Delegates_Uialertview_Uialertviewdelegate - Fatal编程技术网

Ios 代表的多个视图

Ios 代表的多个视图,ios,iphone,delegates,uialertview,uialertviewdelegate,Ios,Iphone,Delegates,Uialertview,Uialertviewdelegate,目前,我有一个类在各处弹出UIAlertViews。目前,相同的类是这些类的委托(这是非常合乎逻辑的)。不幸的是,这些UIAlertViews将调用类的相同委托方法。现在,问题是-如何知道从哪个警报视图调用委托方法?我想检查一下alert视图的标题,但这并不是很优雅。处理多个UIAlertViews最优雅的方式是什么?如下标记UIAlertViews: #define kAlertViewOne 1 #define kAlertViewTwo 2 UIAlertView *alertView1

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

如下标记
UIAlertView
s:

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

仅供参考,如果您只想针对iOS4用户(这是合理的),那么您应该能够使用块对UIAlertView进行非常好的内联处理

这里有一个Stackoverflow问题来解释它:

我试着用Zachary Waldowski的BlocksKit框架来做这个。他的推荐信看起来很不错。然而,我试图跟进我的项目,但不幸的是,我无法让它工作


因此,正如Berk Güder所建议的,我现在已经使用了
UIAlertView
标记。但在将来的某个时候,我会尝试使用块(最好是支持开箱即用的弧形块)

通过增强UIAlertView以使用块回调,您可以克服这一困难,并防止自己使用标记。看看我写的关于这个主题的文章。

更容易更新

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

全部完成

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

相反,我使用如下属性:

#define kAlertViewOne 1
#define kAlertViewTwo 2

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

UIAlertView *alertView2 = [[UIAlertView alloc] init...
alertView2.tag = kAlertViewTwo;
在接口部分:

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

啊,很好。虽然我会使用开关。:)当然,开关也可以工作,但我从来不喜欢开关回答得很好。但这让我想到了另一个问题。为什么要定义kAlertViewOne 1和kAlertViewRwo 2。您不能只在alertView.tag=1或alertView.tag=2中使用吗?这样做是有原因的吗?kAlertViewOne和kAlertViewTwo比1或2更有意义。例如,在您的问题中,您可以使用kAlertResume和kAlertRetry,它们比两个随机数更具可读性。仅供参考,分配数字小于10的标签可能与Apple很少使用的标签冲突。如果您使用的是iOS 8+,UIAlertController具有基于块的委托方法。