Present a view controller, dismiss it and present a different one in Swift

前端 未结 1 738
广开言路
广开言路 2020-12-08 07:43

So I have a root view controller that has a button that when the user pushes it, another view controller is presented. This second controller has a dismiss option that just

相关标签:
1条回答
  • 2020-12-08 08:19

    The error occurs because you are trying to present SecondController from FirstController after you have dismissed FirstController. This doesn't work:

    self.dismiss(animated: true, completion: {
        let vc = SecondController()
    
        // 'self' refers to FirstController, but you have just dismissed
        //  FirstController! It's no longer in the view hierarchy!
        self.present(vc, animated: true, completion: nil)
    })
    

    This problem is very similar to a question I answered yesterday.

    Modified for your scenario, I would suggest this:

    weak var pvc = self.presentingViewController
    
    self.dismiss(animated: true, completion: {
        let vc = SecondController()
        pvc?.present(vc, animated: true, completion: nil)
    })
    
    0 讨论(0)
提交回复
热议问题