views:

75

answers:

2

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 :/

+2  A: 

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.

TechZen
A: 

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

Madhup
You should use `self` notation when referring to properties and you should not release a property except in `dealloc`. See my edit. The way you had it originally is fragile and asking for trouble.
TechZen
thats a great solution but I have to run that like every second, I could I somehow *Observer* that variable?
DotSlashSlash