【问题标题】:Trying to pass data to container view with segue not working尝试将数据传递到容器视图而 segue 不起作用
【发布时间】:2017-05-02 12:54:43
【问题描述】:

我正在尝试通过准备 segue 将用户 ID 传递给容器视图

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "childSegue" {
        if let destinationVC = self.childViewControllers.first as? InterestCollectionController {
            destinationVC.currentUser = self.currentUser
        }
    }
}

但是,这不起作用。我的故事板看起来像这样。 storyboard 我也尝试过使用 segue.destination 作为! InterestCollectionController 这也不起作用。据我了解,这是我应该将数据传递到我的容器视图的方式。感谢任何帮助!

【问题讨论】:

  • 写得好像让destinationVC = segue.destinationViewCONtroller 一样? InterestCollectionController {destinationVC.currentUser = self.currentUser }
  • 在容器视图中,我试图打印出 id 以确保它正确传递,但是在展开可选值时意外发现错误。
  • 你在'self.currentUser'中有任何用户ID吗?请检查。
  • 我在 self.currentUser 中有一个值。
  • ok.. 请在您的目标类中检查“currentUser”。您是否已标记为可选?如果是,那么请在您的目标类中将其作为非选项说 var myString = String()

标签: ios swift segue uicontainerview


【解决方案1】:

不使用 if 语句试试这个:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "childSegue" {
            let destinationVC = segue.destinationViewController as InterestCollectionController
            destinationVC.currentUser = self.currentUser

        }
     }

1.确保您的 segue 标识符与情节提要标识符匹配。

2。一旦你在情节提要中对上述控制器进行了精确的segue,也要进行验证。

【讨论】:

    【解决方案2】:

    试试这个

    var destinationVC : InterestCollectionController?
    
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if (segue.identifier == "childSegue") {
                self.destinationVC = segue.destination as? InterestCollectionController
                destinationVC.currentUser = self.currentUser
            }
        }
    

    【讨论】:

      【解决方案3】:

      我测试了嵌入式视图控制器的视图没有加载,也就是说,在调用containerVC的prepare()之前没有调用viewDidLoad()

      因此,您不能在 prepare() 函数中直接将数据传递给目标视图控制器。

      【讨论】:

      • 所以你的基础视图控制器中的 prepare() 被首先调用,然后你的嵌入视图被加载,最后你的基础视图被加载。考虑到执行顺序,您会发现将数据传递给嵌入式视图的正确位置是在基本视图控制器的 viewDidLoad() 中。您可以像这样获取嵌入式视图控制器的实例:“if let childVC = self.children.first as?ChildViewController { ...”,然后您就有机会传递数据。
      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      相关资源
      最近更新 更多