我已经在论坛上搜索了这方面的信息,但没有找到关于这个错误的信息。我在xcode中创建了一个简单的单视图应用程序,并在viewDidLoad方法中添加了创建测试警报的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert Test"
message:@"Alert Test"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alert show];
// Do any additional setup after loading the view, typically from a nib.
}
我没有添加任何其他代码。当我在iphone4上运行这个程序时,它运行起来没有任何问题。但当我点击iphone4的主页按钮并显示警报时,我会收到上面提到的错误。
设备:iphone4操作系统:iOS 5.1.1
请帮忙。提前感谢
您不应该在viewDidLoad中显示alertView
,[UIAlertView show]
使用动画显示alertView。当我们不在屏幕/视图中时,我们应该避免动画表演,而是在viewDidAppear
中显示alertView
。
编辑:
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismiss) name:@"UIApplicationWillResignActiveNotification" object:nil];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
alert = [[UIAlertView alloc]initWithTitle:@"test" message:@"test" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
}
//do not forget to add this in your header
-(void)dismiss{
[alert dismissWithClickedButtonIndex:-1 animated:NO];
}