How to prevent present modally an active controller?

前端 未结 4 1009
情歌与酒
情歌与酒 2021-01-11 16:11

We have 2 controllers: MainVC and ProfileVC. From MainVC we go to ProfileVC with profileButton press (left item on navigation bar).

In Profile VC we hav

相关标签:
4条回答
  • 2021-01-11 16:45

    I have found kind of solution for this

    - (IBAction)menuButtonPressed:(id)sender
    {
        [self.parentViewController presentViewController:self.menuAlert animated:YES completion:nil];
    }
    

    Alert view sometimes appears on menuVC, but it doesn't crash application and work normally. Answer was found there: Warning :-Presenting view controllers on detached view controllers is discouraged

    0 讨论(0)
  • 2021-01-11 16:47

    I think u will need to dismiss the LAST viewcontroller first before going back by present modal segue, the viewcontroller is active, so it crash:

    Use this after call segue from mainVC or something u wanted to go back:

    [self dismissViewControllerAnimated:NO completion:nil]
    

    or

    [[self presentingViewController] dismissViewControllerAnimated:NO completion:nil]
    

    or use push segue, it auto add a back button that automatically add back function

    0 讨论(0)
  • 2021-01-11 16:48

    There may be another case for other developers:

    Is the target ViewController presented after a connection triggered by a button? The user may be clicking twice, making two connections and opening the same ViewController twice, if you have a shared instance of the said ViewController.

    Prevent it by doing something like this:

    var didSuccess = false
    
    func success(result: LoginModel.Result) {
        if didSuccess {
            return
        }
        didSuccess = true
        present(MainTabBarController.sharedInstance, animated: true, completion: nil)
    }
    

    Or disabling the button during connection.

    0 讨论(0)
  • 2021-01-11 16:51

    To prevent this exception. Simply just dismiss the currently showed ViewController to go back to the caller of the ViewController that you are dismissing:

    self.dismiss(animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题