当应用程序从后台

时间:2015-07-01 06:27:05

标签: ios objective-c iphone

目前我在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];
}

5 个答案:

答案 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{    
}