dismiss a modalviewcontroller when the application enters background

ⅰ亾dé卋堺 提交于 2019-12-22 11:18:13

问题


i need to dismiss my uiimagepicker modal viewcontroller automatically when the application enters the background.i tried to put the code to dismissmodalviewcontroller code in viewdiddissappear method,but its not being called.so i made a reference to the viewcontroller in appdelegate and tried to put it in the applicationdidenterbackgroundmethod but still it is not working.can someone point out the right way to do this


回答1:


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];
}



回答2:


Best way to remove the modal when app is moving to background and it works fine .

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dismissView:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:nil];
} 

- (void)dismissView:(id)sender {
     [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {

     [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Also you can remove observer like this

 [[NSNotificationCenter defaultCenter] removeObserver: self
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];



回答3:


I don't think you need to go through all that.

From the docs:

If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.

Try calling [self dismissModalViewController:NO] from the parent view controller in your implementation of - (void) viewDidUnload.

This is untested, but the docs imply that it should do the job for you.



来源:https://stackoverflow.com/questions/5685419/dismiss-a-modalviewcontroller-when-the-application-enters-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!