目前我在UIAlertView
上显示viewDidLoad
,但是一旦进入后台并重新启动,它就不会再出现了。我该如何解决这个问题?我需要哪些代表,我该怎么做呢?
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"This is an Alert!" delegate:nil cancelButtonTitle:@"Cancel!" otherButtonTitles:nil, nil];
[myAlert show];
}
答案 0 :(得分:1)
apple doc:UIApplicationDidBecomeActiveNotification
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// don't forget remove it
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)yourMethod
{
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"This is an Alert!" delegate:nil cancelButtonTitle:@"Cancel!" otherButtonTitles:nil, nil];
[myAlert show];
}
答案 1 :(得分:1)
如果您想在应用程序进入Foreground或变为活动状态时只显示一次警报,则可以使用以下Appdelegate方法编写警报代码。
applicationDidBecomeActive
答案 2 :(得分:0)
使用viewWillAppear
方法显示提醒,如果您需要在每次出现视图时执行此操作
答案 3 :(得分:0)
您可以覆盖viewDidLoad
并订阅UIApplicationDidBecomeActiveNotification
:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAlert:) name:UIApplicationDidBecomeActiveNotification object:nil];
[self showAlert:nil];
}
- (void)showAlert:(NSNotification *)noti {
[[[UIAlertView alloc] initWithTitle:@"Test" message:@"This is a test" delegate:nil cancelButtonTitle:@"Done" otherButtonTitles: nil] show];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
答案 4 :(得分:0)
AppDelegate.m有默认的委托方法,可以使用它。
- (void)applicationWillEnterForeground:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}