代码之家  ›  专栏  ›  技术社区  ›  quano

代表的几个uiAlertView

  •  38
  • quano  · 技术社区  · 14 年前

    现在我有一个班要开课了 UIAlertView 到处都是。目前,同一个类是这些的委托(这是非常合乎逻辑的)。不幸的是,这些 uiAlctVIEW s将调用类的相同委托方法。现在,问题是-您如何知道从哪个警报视图调用委托方法?我正想检查一下警报视图的标题,但这并不是那么优雅。最优雅的处理方法是什么 S?

    5 回复  |  直到 9 年前
        1
  •  102
  •   Can Berk Güder Pugalmuni    14 年前

    标记 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) {
            // ...
        }
    }
    
        2
  •  4
  •   Community kfsone    7 年前

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

    下面是一个StackOverflow问题,解释它:
    Block for UIAlertViewDelegate

    我尝试使用Zachary Waldowski的blockskit框架。伊斯 UIAlertView(BlocksKit) API 参考看起来很不错。然而,我试图跟随 his instructions to import the BlocksKit framework 进入我的项目,但不幸的是我不能让它工作。

    所以,正如Berk G_¼der所暗示的,我已经使用了 UIAlertView 现在的标签。但在将来的某个时候,我会尝试使用块(最好是支持开箱即用的弧的块)!

        3
  •  3
  •   nima sp    11 年前

    更容易更新

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

    都做完了!

        4
  •  1
  •   Stavash    12 年前

    您可以通过增强uialertview来使用块回调,从而克服这整个难题并防止自己使用标记。退房 this blog post 我就这个问题写过信。

        5
  •  0
  •   Marcus Adams    9 年前

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

    相反,我使用如下属性:

    在接口部分:

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