【问题标题】:different alert views and viewcontroller不同的警报视图和视图控制器
【发布时间】:2011-07-27 13:31:57
【问题描述】:

在同一个视图控制器中,我必须通过警报按钮触发的不同操作显示不同的警报(此视图控制器是警报的代表)。

考虑到在

中,有没有办法重用警报代码init/show/release
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

我需要区分警报。

【问题讨论】:

    标签: iphone objective-c uiviewcontroller uialertview code-reuse


    【解决方案1】:

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

        - (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
    //}
        }
    

    【讨论】:

    • 所以,如果我将警报初始化/显示/发布代码放在一个方法中,我可以传递一个标签(我想这是一个 NSString*),然后在方法委托中检查标签是否相等?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      enum {
          MyFirstTypeOfWarning,
          MySecondTypeOfWarning
      };
      typedef NSInteger SPAlertViewIdentifier;
      

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

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

      然后,在alertView:clickedButtonAtIndex:中可以查看传入的alert view的tag并做出相应的响应。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2011-08-12
        • 2016-03-10
        相关资源
        最近更新 更多