How to observe when a UIAlertView is displayed?

こ雲淡風輕ζ 提交于 2019-12-13 02:55:57

问题


Is it anyway possible to observe if a UIAlertView is being displayed and call a function when it is.

The UIAlertView is not being created and displayed in the same class which I want a function to be called in.

Its hard to explain but to put it simply I need to somehow monitor or observe if the view becomes like out of first responder or something because I dont think it is possible to monitor if a UIAlertView is being displayed :/


回答1:


Sounds like a job for notifications.

Say that class A creates the UIAlert and class B needs to observe it. Class A defines a notification. Class B registers for that notification. When Class A opens the alert it post the notification and class B will see it automatically.

See NSNotification for details.




回答2:


You can do it like (if you don't want to perform an event or generate a notification and just want to check if the alert is displayed or not), declaring a alertview as a class level variable and releasing it when your alertview is dismissed.

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

Now you can check it by:

if(viewControllerAClassObj.theAlertView){

}  

Hope this helps,

Thanks,

Madhup



来源:https://stackoverflow.com/questions/2599590/how-to-observe-when-a-uialertview-is-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!