简体   繁体   中英

iOS - Stop Modal UIViewController from getting presented

I have a modal UIViewController . On receiving a low memory warning, I want to close that modal. For this purpose, I wrote the following code -

- (void)didReceiveMemoryWarning
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [super didReceiveMemoryWarning];
}

This works fine in most cases. However, when I have initialized the view controller, but not yet presented it on the screen, and a memory warning occurs at that time, then the code execution continues, the modal does not get dismissed because it is not yet presented, and when my execution reaches the point where it gets presented, the modal still shows up.

How do I go about handling this scenario and prevent the modal from being presented? Please let me know if something is not clear - I will try to explain it better.

can you check results according to below code...

- (void)didReceiveMemoryWarning
{
    if(!self.myVC)
    {
     [self presentViewController:myVC animated:NO 
     completion:
               { [self dismissViewControllerAnimated:NO completion:nil];}];
    }
    else
     [self dismissViewControllerAnimated:YES completion:nil];
}

When you receive memory warning and if your modal is not presented then you can release the modal view controller and equate it to nil. You can set one bool after you present the modal view and make use of that in didReceiveMemoryWarning to dismiss the already presented modal view.

-(void)didReceiveMemoryWarning
{
    if(!_isModalPresnted)
    {
        [_modalViewController release]
        _modalViewController = nil;
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

So when your execution reaches the point where it gets presented you can have a check

if(_modalViewController)
{
      _isModalPresnted = YES;
     [self presentViewController:_modalViewController animated:YES completion:nil];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM