IB 中的 tabBar viewControllers:发送自定义 init?
我的基于 tabBarController 的应用程序有几个选项卡。 每个都有一个自定义 viewController 类,唯一的区别是实例的初始化方式。 有没有办法让界面生成器发送不同的选项卡自定义初始化参数?
目前我正在 viewWillAppear 中进行初始化,但出于多种原因,在 IB 中而不是在代码中进行初始化是有意义的。
有什么建议么?
谢谢, 凯尔索
My tabBarController-based app has several tabs. Each has a custom viewController class, the only difference being the way the instance is initialized. Is there a way to make interface builder send the different tabs custom init parameters?
Currently I'm doing the initialisation in viewWillAppear, but for a bunch of reasons it would make sense to do it in IB instead of in the code.
Any suggestions?
thanks,
Kelso
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Interface Builder 创建一个对象存档,当程序执行时该存档将取消存档。 你不能真正告诉 IB 调用特定的方法。
如果需要在调用
viewWillAppear:
之前进行初始化,可以在awakeFromNib
中进行初始化,保证在所有对象加载完毕且所有插座连接完毕后调用到他们的目标。如果您想更早地进行初始化,可以通过覆盖
initWithCoder:
来实现(有关文档,请参阅NSCoding
协议)。 我不知道它是否在任何地方都有记录,但这是为从存档中解码的对象指定的初始化。在上述所有内容中,您将无法接收参数,但在代码中,您应该能够通过明智地使用全局变量来访问您需要的任何内容。 您还可以使用
[[UIApplication sharedApplication] delegate]
来访问您的应用程序委托对象。Interface Builder creates an archive of objects that is unarchived when you program executes. You can't really tell IB to call particular methods.
If you need to initialize before
viewWillAppear:
is called, you can do so inawakeFromNib
, which is guaranteed to be called after all objects have been loaded and all outlets have been connected to their targets.If you want to do initialization even earlier, you can do so by overriding
initWithCoder:
(see theNSCoding
protocol for documentation). I don't know if it is documented anywhere, but that is the designated initialized for objects being decoded from an archive.In all of the above, you won't be able to receive parameters, but in the code you should be able to access whatever you need with some judicious use of global variables. You can also use
[[UIApplication sharedApplication] delegate]
to get access to your application delegate object.我认为没有任何方法可以更改加载 nib 时 IB 运行时调用的方法。 如果您描述了您想要完成的任务(即为什么在 viewDidAppear 中进行设置对您不起作用),您可能会得到处理初始化的更好方法的建议。
I don't think there's any way to change what methods are called by the IB runtime when your nib is loaded. If you described what you were trying to accomplish (i.e. why doing the setup in viewDidAppear doesn't work for you), you might get a suggestion of a better way to handle your initialization.