且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序进入后台时关闭modalviewcontroller

更新时间:2023-01-09 23:21:05

尝试在要关闭的UIViewController中为UIApplicationDidEnterBackgroundNotification添加NSNotificationCenter观察器.使用选择器关闭模态视图

Try to add an NSNotificationCenter observer for UIApplicationDidEnterBackgroundNotification in the UIViewController that you want to dismiss. Use the selector to dismiss the modalview

- (void)viewWillAppear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(didEnterBackground:) 
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter] removeObserver: self
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];
}

- (void)didEnterBackground:(NSNotification*)note
{
  [self.navigationController dismissModalViewAnimated:NO];
}