Adding An AlertView At viewWillAppear, iPhone
I am attempting to show an alert vie开发者_开发知识库w 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
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:
Have the alert code be in
viewDidAppear
, rather thanviewWillAppear
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];
}
Try putting the alert in viewDidAppear.
Also, what is the iboutlet for? Try taking the whole thing out.
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.
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.
精彩评论