在 viewDidLoad 之前将变量传递给视图控制器 [英] Pass variable to view controller before viewDidLoad

查看:71
本文介绍了在 viewDidLoad 之前将变量传递给视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行在 Storyboard 中定义的 segue 以打开一个新的视图控制器.我需要在不需要显示某些按钮的特殊状态下配置 segue 的目标视图控制器.

I perform a segue which is defined in the Storyboard to open a new view controller. I need to configure the destination view controller of the segue in a special state where some of it's buttons does not needed to be displayed.

我知道我可以通过在我的源视图控制器的 -prepareForSegue:sender: 中在这个控制器上设置一个变量来做到这一点.问题在于,它首先实例化控制器,因此 -viewDidLoad: 将运行,然后我才能在其上设置任何内容.

I know that I can do this by setting a variable on this controller in my source view controller's -prepareForSegue:sender:. The problem with this is, that firstly it instantiates the controller, so it's -viewDidLoad: will run, then only after can I set anything on it.

我无法完全从代码创建控制器,因为它的用户界面在 Storyboard 中.-instantiateViewControllerWithIdentifier: 显然也首先调用了 -viewDidLoad.

I can't create the controller entirely from code, because it's user interface is in Storyboard. -instantiateViewControllerWithIdentifier: also calls -viewDidLoad first obviously.

我可能可以使用信号量并将初始化代码添加到我的目标控制器的 -viewWillAppear 中,但这很丑陋,它必须是一种比每次执行检查更优雅的方法来做到这一点视图出现.(我的设置只需要做一次.)

I could probably use a semaphore and add the initialization code into my destination controller's -viewWillAppear, but that's ugly, it has to be some more elegant way to do this than doing a check every time the view appears. (My settings need to be done only once.)

有没有办法在 -viewDidLoad 运行之前将变量传递给控制器​​?

Is there some way to pass variables into the controller before it's -viewDidLoad runs?

EDIT:看起来只有当我使用 -performSegueWithIdentifier: 从代码触发 segue 时才会发生这种情况.

EDIT: It looks like this happens only if I trigger the segue from code using -performSegueWithIdentifier:.

推荐答案

我认为这里有些混乱.-prepareForSegue:sender:-viewDidLoad 被调用之前被调用.请仔细检查您的实施情况.

I think there is some confusion here. -prepareForSegue:sender: gets called before -viewDidLoad gets called. Please double check your implementation.

可能是此主题将帮助您理解这一点和上述案例之一属于你的情况.

May be this thread will help you understand this and one of the mentioned cases fall in your case.

这篇关于在 viewDidLoad 之前将变量传递给视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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