【问题标题】:Failed dismiss 2 view controller关闭 2 视图控制器失败
【发布时间】:2018-10-13 08:40:23
【问题描述】:

我有 3 个 ViewController:LoginViewController、CheckinViewController 和 ProfileViewController

流程是: LoginVC --> CheckinVC --> ProfileVC

我需要的是: 单击“ProfileVC”中的注销按钮时,我想关闭“ProfileVC”和“CheckinVC”,然后返回“LoginVC”

LoginVC.swift

let checkinViewController = self.storyboard?.instantiateViewController(withIdentifier: "CheckinViewController") as! CheckinViewController                    
self.navigationController?.pushViewController(checkinViewController, animated: true)
JustHUD.shared.hide()
self.dismiss(animated: false, completion: nil)

CheckinVC.swift

if let profileView = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewController") {
        profileView.providesPresentationContextTransitionStyle = true
        profileView.definesPresentationContext = true
        profileView.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext;
        //            profileView.view.backgroundColor = UIColor.init(white: 0.4, alpha: 0.8)
        profileView.view.backgroundColor = UIColor.clear
        profileView.view.isOpaque = false
        self.present(profileView, animated: true, completion: nil)

这是我正在尝试做的事情

ProfileVC.swift

@IBAction func clickLogout(_ sender: Any) {
    UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
    UserDefaults.standard.synchronize()

    self.dismiss(animated: false, completion: {
        print("ProfileView : dismiss completed")

        let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
        self.navigationController?.pushViewController(loginViewController, animated: true)

        self.dismiss(animated: false, completion: {
            print("SUCCESS")
        })

    })
}

【问题讨论】:

    标签: ios iphone swift xcode swift4


    【解决方案1】:

    你需要做一个Unwind Segue,这样你就可以回到你的LoginVC。 按照以下简单的四个步骤创建Unwind segues

    1. 在您尝试返回的视图控制器中,在您的示例中为LoginVC,编写以下代码:

      @IBAction func unwindToVC1(segue:UIStoryboardSegue) { }
      

    ( 记住:在视图中插入这个方法很重要 您正试图返回的控制器! )

    1. Storyboard 中,转到您要从中放松的屏幕(在我们的例子中为 ProfileVC),然后控制并拖动 viewController 图标到顶部的退出图标。

    3.到Storyboard中选中viewController的文档大纲,如下图选择the unwind segue

    现在,转到Utilities Pane 中的Attributes Inspector 并命名展开转场的标识符。

    1. 最后,在您希望触发 unwind segue 动作的地方编写此代码,在我们的例子中为 ProfileVC

       @IBAction func clickLogout(_ sender: Any) {
              UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
                UserDefaults.standard.synchronize()
                  performSegue(withIdentifier: "unwindSegueToVC1", sender: self)
       }
      

    更多信息请查看Create Unwind Segues

    【讨论】:

    • 我不能 ctrl + 从“Icon vc”拖到“Icon exit”,有什么问题吗?
    【解决方案2】:

    虽然如果您想使用故事板,Enea Dume 的解决方案是正确的,但这里是对问题的解释,以及如果您想像迄今为止那样在代码中执行此操作的解决方案。

    问题

    如果我们关注 ProfileVC 中 logoutFunction 中的 self.dismiss 调用,就会发生这种情况。

    第一次调用 self.dismiss 时,ProfileVC 将自行关闭并从视图堆栈中删除。

    在完成委托中,您将新的 LoginVC 推送到导航控制器。但是,CheckIN VC 显示在导航控制器上,因此您看不到任何事情发生。

    对 self.dismiss 的第二次调用什么也不做,因为 ProfileVC 没有呈现任何其他视图控制器,并且它不再在堆栈中。

    解决方案

    您需要保留对提供 CheckInVC 的 LoginVC 的引用。如果您调用“对 LoginVC 的引用”.dismiss,它将在堆栈中关闭其上方的视图控制器并带您返回登录视图控制器。

    【讨论】:

      【解决方案3】:

      在 CheckinVC.swift 类中,在函数 viewDidAppear 中,根据您正在维护的会话以及相应的弹出登录视图控制器检查用户是否仍然处于活动状态。如果用户状态为注销,那么它将转到登录视图控制器。否则它将照常工作。

      【讨论】:

        【解决方案4】:

        问题是您试图关闭两次 ProfileVC,但您真正需要的是关闭它然后弹出 CheckinVC。

        此外,在关闭视图控制器后,它不再具有对 NavigationController 的引用,因此您需要在时间变量中对其进行引用。

        像这样更改 ProfileVC:

        @IBAction func clickLogout(_ sender: Any) {
            UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
            UserDefaults.standard.synchronize()
        
            let navigationController = self.navigationController
            let loginViewController = storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
            self.dismiss(animated: false, completion: {
                print("ProfileView : dismiss completed")
                navigationController?.pushViewController(loginViewController, animated: true)
                navigationController?.popViewController(animated: false)
            })
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-06
          • 1970-01-01
          • 2015-03-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多