开发者

How to observe when a UIAlertView is displayed?

开发者 https://www.devze.com 2022-12-26 17:15 出处:网络
Is it anyway possible to observe if a UIAlertView is being displayed and call a function when it is.

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 sim开发者_运维技巧ply 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 :/


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消