【问题标题】:Trouble with back button后退按钮有问题
【发布时间】:2017-03-08 11:36:16
【问题描述】:

我的后退按钮有奇怪的问题。我有2个VC。每个 VC 都有自己的后退按钮(它们不是默认的,它们被添加为左栏按钮项。当我从 VC1(与主视图控制器链接,未显示)到 VC2 时,我看到了 VC2,但是当我回来时到 VC1 我看到 VC1 的导航栏和 VC2 的视图。我需要做什么? 我也尝试添加 func 但没有帮助。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "comeBack" {
        let backVC = self.storyboard?.instantiateViewController(withIdentifier: "CollectionVC") as! RecipeCollectionViewController
        self.navigationController?.pushViewController(backVC, animated: true)
    }
}

【问题讨论】:

  • 为什么要为 First 控制器设置后退按钮?
  • 它不是第一控制器。它与 Home View Controller 链接
  • 所以当你点击VC2的自定义后退按钮时,你是否编写了问题中显示的代码?如果是这种情况,你不应该推动你的控制器,而应该弹出你的控制器。
  • 从第二个到第一个的错误方式,你不要创建另一个segue,喜欢下面的答案
  • 返回你只需要“self.navigationController?.popViewControllerAnimated(true)”,它就会回到之前的viewcontroller

标签: ios swift view uiviewcontroller


【解决方案1】:

用这个将 VC1 推到 VC2

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as! VC2
self.navigationController?.pushViewController(vc, animated: true)

并将其用于从 VC2 到 VC1 的返回

self.navigationController?.popViewControllerAnimated(true) 

【讨论】:

  • 我把VC1推到VC2没有问题
  • self.navigationController?.popViewControllerAnimated(true) 在你的后退按钮中使用这个
【解决方案2】:

用于弹出特定视图控制器

 let viewControllers: [UIViewController] = self.navigationController!.viewControllers
  for vc in viewControllers {
  if VC is YourViewController {
    self.navigationController!.popToViewController(vc, animated: true)
 }
}

如果弹出前一个控制器而不是使用

self.navigationController?.popViewControllerAnimated(true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2011-01-15
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多