Henry F
Henry F

Reputation: 4980

`UIAlertView` appears multiple times

I display an alert view in the didConnect method for bluetooth. For some reason, it fires four times. I'm trying to bandaid it and it is not working too well. Basically, I put the alert view in a method of its own, and call that method in didConnect. That's when it fires four times. I'm trying to set it up to only fire once. What I tried to do was set the alert views method to return a TRUE value. Then I do this:

if ([successfulConnection self] == FALSE) {
        [self successfullConnection];

    }

This works great the first time, but then the method is set to TRUE for the remainder of the time. I have the feeling that if I set it back to equal FALSE at the end of the if statement, then it will fire four times and I'll be right back where I started. Does anyone know how to change the above code to have it only fire once when it tries to fire four times?

Also tried replacing the above code with this in my didConnect, but it never fired at all:

[successfulConnection self];

if (successfulConnection.visible == YES) {
[successfulConnection dismissWithClickedButtonIndex:0 animated:YES];
}

Upvotes: 0

Views: 2112

Answers (3)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

These are methods you can use according to your requirement:

EDIT : Perfect way... if you dont want to upload your app on App Store

To know that alertView is currently visible or not.

Usage : Display alertView only if necesary other its already present.

-(UIAlertView *)getLastAlertView
{
  Class UIAlertManager = objc_getClass("_UIAlertManager");
  UIAlertView *topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)];
  return topMostAlert;
}

Dissmiss any alertView present which you don't know.

Usage : dissmiss all alertView and then present new one

-(void)dissmissLastAlert
{
  Class UIAlertManager = objc_getClass("_UIAlertManager");
  UIAlertView *topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)];
  if (topMostAlert) {
    [topMostAlert dismissWithClickedButtonIndex:0 animated:YES];
  }
}

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

If you call successfulConnection from your didConnect method, I think this should work (myAlert is the property name for the alert view):

-(void)successfulConnection {
    if (! self.myAlert) {
        self.myAlert = [[UIAlertView alloc]initWithTitle:@"ttile" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: nil];
        [self.myAlert show];
    }
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    self.myAlert = nil;
    //do whatever with the result
}

Upvotes: 6

bengoesboom
bengoesboom

Reputation: 2147

The simplest thing to do is to just have a boolean that gets set to true when the UIAlertView is displayed, and then false when the UIAlertView is dismissed. Then, whenever you are going to show the UIAlertView, first check if it is already displaying.

Upvotes: 0

Related Questions