如何在将数据推送到导航堆栈之前将数据传递给视图控制器并加载它? [英] How can I pass data to a viewcontroller and load it before pushing it onto Navigation stack?

查看:15
本文介绍了如何在将数据推送到导航堆栈之前将数据传递给视图控制器并加载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有集合视图的视图控制器,当我单击一个单元格时,我将一个变量传递给另一个视图控制器,然后将其推送到导航堆栈上.

I have a view controller with a collection view and when I click a cell, I pass a variable to another view controller, then push it onto the navigation stack.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     self.performSegueWithIdentifier("nextVC", sender: self.userArray[indexPath.row])
}

这是我发送变量的方式:

This is how I send the variable:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     super.prepareForSegue(segue, sender: sender)
     let nextVC = segue.destinationViewController as! NextVC
     if let senderId = sender!.objectForKey("uId") {
          nextVC.userId = senderId as! String
     }
}

下一个视图控制器 (nextVC) 上有很多东西.我已经尽我所能优化它,但加载所有内容仍然需要一些时间.有没有办法可以为segue做准备,传递变量,然后让下一个vc做它的事情,然后当它完成加载时,实际上segue到它?(或显示它)?我看到了一些其他关于预加载视图控制器的问题,但没有一个涉及传递变量.这个变量非常重要,因为 nextVC 中的所有东西都必须知道这个变量(基于单元格触摸),否则将无法加载.我希望我解释得足够好,感谢您的时间.

This next view controller (nextVC) has a lot of stuff on it. I have done everything I can to optimize it but it still takes some time to load all the stuff. Is there a way I can prepare for segue, pass the variable, and let the next vc do it's thing, then when it's done loading, actually segue to it? (or show it)? I saw some other questions which talk about preloading view controllers but none of them dealt with passing a variable. This variable is very important because all the stuff in the nextVC has to know this one variable (based on cell touched) or it won't be able to load. I hope I explained this well enough, thanks for your time.

推荐答案

如果访问destinationViewControllerview属性,会触发view 加载.由于您并不真正需要 view,只需将其分配给 _ 即可让编译器满意.

If you access the view property of the destinationViewController, it will trigger the view to load. Since you don't really need the view, just assign it to _ to keep the compiler happy.

试试这个:

 if let senderId = sender!.objectForKey("uId") {
      nextVC.userId = senderId as! String
      _ = nextVC.view
 }

这将触发 viewDidLoad 在 segue 发生之前运行.确保用于设置 NextVC 的代码位于 viewDidLoad 中,您应该可以开始使用了.

This will trigger viewDidLoad to run before the segue happens. Make sure your code to set up your NextVC is in viewDidLoad and you should be good to go.

这篇关于如何在将数据推送到导航堆栈之前将数据传递给视图控制器并加载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆