【问题标题】:How to present another view controller after dismiss from navigation controller in swift?如何在快速从导航控制器中解散后呈现另一个视图控制器?
【发布时间】:2018-09-11 19:12:36
【问题描述】:

如何在快速从导航控制器中解散后呈现另一个视图控制器?

我正在使用导航控制器。

ViewController1.swift

func pushTo(viewController: UIViewController) {
        let showLocalPwd = self.storyboard?.instantiateViewController(withIdentifier: "LocalPwdVC") as! LocalPwdVC
        self.navigationController?.present(showLocalPwd, animated: true, completion: nil)
}

ViewController2.swift

    @IBAction func btnVerify(_ sender: Any)
    {
            self.dismiss(animated: true, completion: {
                 let vc = self.storyboard.instantiateViewController(withIdentifier: "DataVC") as! DataVC
            self.navigationController.pushViewController(vc, animated: true)
            })
    }

关闭视图控制器后,它不会转到下一个视图控制器,即 DataVC

【问题讨论】:

标签: ios swift uinavigationcontroller


【解决方案1】:

如果你想展示视图控制器,那么在你的dismissViewController中创建一个协议

protocol dismissViewController {
func presentCompletedViewController()
 }
// Then create a delegate 
   var delegate = dismissViewController? = nil
// If you are using action to dismiss your ViewController then call delegate

     @IBAction func YourButton(_ sender: Any) {
    self.dismiss(animated: true) {
        self.delegate!.presentCompletedViewController()
    }
}

//And then call this in your main or homeViewController
  class HomeViewController: UIViewController, dismissViewController {
 func presentCompletedViewController(){
// enter code to present view controller
 }
 // And at last don't forget to call delegate
    yourVc.delegate = self
    you need to call this delegate in which you are presenting your dismissViewController

【讨论】:

  • 问题出在哪里
  • 你需要调用这个函数来调用你的dismiss view controller let vc = self.storyboard?.instantiateViewController(withIdentifier: "dismissVC") as! dismissViewController vc.delegate = self
  • self.dismiss(animated: true, completion: { self.delegate!.presentVisitorDataVC() }) 。 .... 它显示“在展开可选值时意外发现 nil”。
  • 因为您没有调用委托方法,所以转到您的第一个控制器,您从中调用了关闭视图控制器并将其添加到您的推送序列中
猜你喜欢
  • 2019-09-11
  • 1970-01-01
  • 2018-02-16
  • 2016-09-24
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多