views:

381

answers:

4

Hello community,

I have a UIAlert that pops up 3 times every time it is called. It appears and then disappears before I can click on it. Could it be that the viewDidLoad itself is being called 3 times?

I implemented an UIAlert in the viewDidLoad of my app:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:alertMessage  delegate:self cancelButtonTitle:ok otherButtonTitles:nil];

This is the viewDidLoad in the rootViewController, that manages the views:

- (void)viewDidLoad {
    Kundenkarte *kartenAnsicht = [[Kundenkarte alloc]
                                                initWithNibName:@"Kundenkarte" bundle:nil];
    kartenAnsicht.rootViewController = self;
    kartenAnsicht.viewDidLoad;
    self.map = kartenAnsicht;


    [self.view addSubview:kartenAnsicht.view];


    [kartenAnsicht release];
//  [super viewDidLoad];

}

The viewDidLoad that evokes the UIAlert is in the kartenAnsicht view controller.

I hope someone can help me because I'm out of ideas.

+1  A: 

You don't need to call -viewDidLoad yourself, it's run automatically by the NIB-loading mechanism. That means you get extra invocations of -viewDidLoad: one by design, and extras whenever you call it.

Graham Lee
A: 

Yes ok, but is the Problem that the kartenAnsicht view is called for more times, that i have implemented the code in viewDidLoad() in the rootViewController what can i do to solve the Problem?

Marco
Stackoverflow isn't a forum format. You should place this in a comment under Graham Lee's answer. These answers are just for others trying to solve your problem.
TechZen
ok sorry it didn't know that, but now i will do that
Marco
A: 

First of all, you should never put any type of display in viewDidLoad. That method is intended for behind the scenes configuration after the view is first read from nib. There is no certainty that it will be called every time the view displays because after the first time it loads, the view maybe held in memory and not reloaded from nib.

Instead, put the call to evoke the NSAlert in viewWillDisplay or viewDidDisplay. This will display the alert each time the view appears.

I doubt that viewDidLoad is called three times but to check for that just put an NSLog in the method to see how many times it is called.

When you say that:

i implemented an NSAlert in the viewDidLoad() of my app:

... what does that mean? What object exactly has the method? If it is the application delegate, this will not work because the application delegate protocol does not respond to viewDidLoad. It has to be in a UIViewController.

Edit01:

See this post that had the same problem: http://stackoverflow.com/questions/2273283/uialertview-pops-up-three-times-per-call-instead-of-just-once/2273511#2273511

Short answer: You kill the alert by releasing it. Either retain it as a property of the view controller or better yet, display the alert with runModal instead of show and capture the button number returned immediately.

TechZen
A: 

It would be helpful to see the code around the alert call.

I am using an alert whenever the reachability changes. Since reachability is checked repeatedly, the alert could get called repeatedly. To alleviate that, I wrap the alert code like so:

if (!myAlert) { /* set up and show myAlert */ }

However, one problem with this is that when you click the Cancel button, the alert will remain non-nil, and therefore can never show again because of that conditional. If someone could add to this response with a fix for that, that would be great. I assume I can add a handler for the cancel button that will destroy myAlert.

Rich Joslin
I've found this works even better, and seems to solve the cancel button issue I was having:if (myAlert.superview == nil) { /* set up and show myAlert */ }
Rich Joslin