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

suj*_*406 3 iphone background modalviewcontroller

我需要在应用程序进入后台时自动解除我的uiimagepicker模态视图控制器.我试图将代码放入viewdiddissappear方法中的dismissmodalviewcontroller代码,但它没有被调用.所以我在appdelegate中引用了viewcontroller并试图把它放到在applicationdidenterbackground方法中,但它仍然没有工作.有人指出正确的方法来做到这一点

hon*_*eng 7

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)