views:

152

answers:

4

Hello all, I am attempting to show an alert view as soon as the view appears (without using a button). In viewcontroller.m I have:

- (void) viewWillAppear:(BOOL)animated
{
    UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"User Information" message: @"Hello"
            delegate: self cancelButtonTitle:nil otherButtonTitles: @"Continue", nil];

    [alert show];

    [alert release];
}

and in viewcontroller.h I have:

IBOutlet UIView *alert;

I haven't done anything to my .xib file.

When run on the simulator I get the following in console:

2009-11-30 23:41:36.079 BatteryApp[867:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BatteryAppViewController" nib but the view outlet was not set.'

Any ideas where I have gone wrong, something to do with my xib?

Edit // I connect alert to a new view in my xib, still no luck

A: 

Try putting the alert in viewDidAppear.

Also, what is the iboutlet for? Try taking the whole thing out.

ACBurk
I did as you said, but get the same error
Stumf
+2  A: 

The problem is that the two core animations (the one for viewing the current view, and the one for showing the alert ) overlap. You need to separate these two animation moves, by following this workaround:

  1. Have the alert code be in viewDidAppear, rather than viewWillAppear

  2. Call [alert performSelector:@selector(show) withObject:nil afterDelay:0.0]. This ensures that the alert occurs after finishing the current animation.

You may need to be careful about releasing alert quite yet. If I were you, I would have a separate method (e.g. showHelloAlert), and then call it from viewDidAppear:

- (void)viewDidAppear: (BOOL)animated {
  [self performSelector:@selector(showHello) withObject:nil afterDelay:0.0f];
  [super viewDidAppear:animated];
}
notnoop
Thanks, error gone, and alert is displaying... but 3 times! It displays then automatically disappears. It then re-appears, but requires you to dismiss it before appearing once more!
Stumf
I suspect that you are calling `viewDidAppear:` explicitly multiple times, or that the delegate method calls `showHello` accidentally. You would need to show more code.
notnoop
Specifically what areas of code do you need to see?
Stumf
Fixed now, armed with your code... many thanks!
Stumf
A: 

Open your xib file in Interface Builder, and control drag from the file owner (which should be of class BatteryAppViewController) to your view (not the alert) and select view from the menu that pops up. The log indicates you haven't made this outlet connection.

Johan Kool
A: 

You don't connect UIAlerts up in Interface Builder. Alerts do not have customizable nibs so you can't load them up in interface builder. You need to remove the:

IBOutlet UIView *alert;

...completely.

The error message says that you have no view defined for your view controller. You need to set that up in Interface Builder.

TechZen