【问题标题】:How to observe when a UIAlertView is displayed?如何观察何时显示 UIAlertView?
【发布时间】:2010-04-08 12:00:58
【问题描述】:

是否有可能观察是否正在显示 UIAlertView 并在显示时调用函数。

UIAlertView 没有在我想要调用函数的同一个类中创建和显示。

很难解释,但简单地说,我需要以某种方式监控或观察视图是否变得像第一响应者或其他东西,因为我认为无法监控 UIAlertView 是否正在显示:/

【问题讨论】:

    标签: iphone objective-c uikit uialertview


    【解决方案1】:

    听起来像是通知的工作。

    假设 A 类创建了 UIAlert,B 类需要观察它。 A 类定义了一个通知。 B 类注册该通知。当 A 类打开警报时,它会发布通知,B 类会自动看到它。

    详见 NSNotification。

    【讨论】:

      【解决方案2】:

      您可以这样做(如果您不想执行事件或生成通知并且只想检查是否显示警报),将警报视图声明为类级别变量并在您alertview 被关闭。

      @interface ViewControllerA:UIViewController{
      UIAlertView *theAlertView;
      
      }
      
      @property (nonatomic, retain) UIAlertView *theAlertView;
      @end
      
      @implementation ViewControllerA
      
      @synthesize theAlertView;
      
      - (void)showAlertView{
          // theAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
          // [theAlertview show];
          self.theAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
          [self.theAlertview show];
      }
      
      - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
          // [theAlerView release];
          self.theAlertView=nil; // The synthesized accessor will handle releasing. 
      }
      

      现在您可以通过以下方式进行检查:

      if(viewControllerAClassObj.theAlertView){
      
      }  
      

      希望这会有所帮助,

      谢谢,

      马杜普

      【讨论】:

      • 引用属性时应使用self 表示法,并且不应释放除dealloc 之外的属性。请参阅我的编辑。你最初拥有它的方式很脆弱,而且自找麻烦。
      • 这是一个很好的解决方案,但我必须像每秒一样运行它,我能以某种方式 Observer 那个变量吗?
      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多