How to correctly dismiss a UINavigationController that's presented as a modal?

后端 未结 8 1761
情话喂你
情话喂你 2021-02-01 16:04

In my TabBarViewController, I create a UINavigationController and present it as a modal.

var navController =  UINavigationController()
let messageVC         


        
相关标签:
8条回答
  • 2021-02-01 16:30

    This is how I solve the problem in Objective C.

    You can call dismissViewControllerAnimated:NO on your self.navigationController itself.

    Objective C

    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    

    Swift

    self.navigationController.dismissViewControllerAnimated(false, completion: nil)
    
    0 讨论(0)
  • 2021-02-01 16:35

    You can use the following to correctly dismiss a UINavigationController that's presented as a modal in Swift 4:

    self.navigationController?.popViewController(animated: true)
    
    0 讨论(0)
  • 2021-02-01 16:37

    No need to have member for navController. Use following code to present your MessagesViewController.

    let messageVC = self.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! MessagesViewController
    let pesentingNavigationController = UINavigationController(rootViewController: messageVC)
    self.presentViewController(pesentingNavigationController, animated: true, completion: nil)
    

    Your dismiss view controller code will be

    func swipedRightAndUserWantsToDismiss() {
      self.navigationController.dismiss(animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2021-02-01 16:42

    In Swift 3 this is achieved with:

    self.navigationController?.dismiss(animated: true, completion: nil)
    
    0 讨论(0)
  • 2021-02-01 16:46

    I suggest you use the other initializer for your UINavigationController:

    let messageVC = self.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! MessagesViewController
    let navController = UINavigationController(rootViewController: messageVC)
    self.presentViewController(self.navController, animated: true, completion: nil)
    

    To dimiss, simply do

    func swipedRightAndUserWantsToDismiss() {
      self.navigationController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    0 讨论(0)
  • 2021-02-01 16:51

    Try this

    func swipedRightAndUserWantsToDismiss(){
        self.navigationController.dismissViewControllerAnimated(false, completion:nil);
    }
    
    0 讨论(0)
提交回复
热议问题