UIApplicationDelegate applicationWillResignActive执行方法但不显示效果

时间:2013-04-12 10:53:20

标签: uiapplicationdelegate

出于安全原因,我需要在进入后台之前清除应用程序的状态,而不是在出现前景之前。 applicationWillResignActive中的代码执行但是视图的解除没有显示也没有生效,因为前一个视图仍然存在,并且视图控制器没有从堆栈中移除。 另一方面,applicationWillEnterForeground中的代码执行并生效为 dismissModalViewControllerAnimated实际上是转换视图并从堆栈中删除视图控制器。

我收到了这个警告:

Warning: Attempt to dismiss from view controller <UINavigationController: 0x1e074c90>     while a presentation or dismiss is in progress!

如何强制dismissModalViewControllerAnimated生效并显示视图的转换并从堆栈中删除视图控制器?

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self initializeState];
}

- (void)initializeState
{
    [self.appDelegateDelegate resetApp:self];
}

...

#pragma mark - AppDelegateDelegate

- (void)resetApp:(AppDelegate *)appDelegate
{
    [SVProgressHUD dismiss];
    [[EmployeeAPIClient sharedInstance] logout];
    [self dismissModalViewControllerAnimated:NO];
}

但如果包含在applicationWillEnterForeground中:正常工作

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self initializeState];
}

1 个答案:

答案 0 :(得分:0)

在您要关闭的视图控制器中,尝试使用NSNotification观察者:

-(void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissThisViewController) name:UIApplicationWillResignActiveNotification object:nil];
}

-(void)dismissThisViewController {
    [self dismissViewControllerAnimated:NO completion:nil];
}