0

出于安全原因,我需要在进入后台之前而不是在进入前台之前清除应用程序的状态。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];
}
4

1 回答 1

0

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

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

-(void)dismissThisViewController {
    [self dismissViewControllerAnimated:NO completion:nil];
}
于 2013-06-02T08:20:50.830 回答