【问题标题】:Transition to MainVC gives the empty View without subviews过渡到 MainVC 给出没有子视图的空视图
【发布时间】:2017-12-02 12:14:57
【问题描述】:

我正在使用这种类型的转换到UIViewcontroller

self.navigationController?.popToViewController(MainVC(), animated: false)

结果我想收到带有所有子视图的 MainVC...但我只收到没有任何子视图的空主视图。我做错了什么?

更新: 我已经过渡到 MainVC,但它是空的,只有初始视图,没有所有子视图。是什么原因?

【问题讨论】:

  • 你从哪个函数执行pop?
  • 我从分机调用它

标签: ios swift uiviewcontroller poptoviewcontroller


【解决方案1】:

你应该传递MainVC()的对象

如下图

let objOfMainVC = MainVC()
self.navigationController?.popToViewController(objOfMainVC, animated: false)

如果你使用的是故事板,那么

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let main = storyBoard.instantiateViewController(withIdentifier: "MainVC") as! MainVC // set withIdentifier to your identifier that was set in your storyboard.
self.navigationController?.pushViewController(main, animated: false)

【讨论】:

  • 在这两种情况下,我都转换到 MainVC,但它是空的,只有初始视图,没有所有子视图。是什么原因?
【解决方案2】:

您需要MainVC 的引用。您不能只创建MainVC() 的新实例

if let viewControllers = self.navigationController?.viewControllers {
    for vc in viewControllers {
        if let _ = vc as? MainVC {
            self.navigationController?.popToViewController(vc, animated: true)
            break
        }
    }
}

或者如果您的 MainVC 是导航控制器的根视图控制器。

self.navigationController?.popToRootViewController(animated: true)

【讨论】:

  • 在这两种情况下,我都转换到 MainVC,但它是空的,只有初始视图,没有所有子视图。是什么原因?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 2013-05-01
相关资源
最近更新 更多