使用TabBarController时如何加载多个ViewController?
我有一个带有 7 个自定义 ViewController 的 TabBarController,我想做的是让 TabBarController 在启动时加载其数组中的第一个 ViewController 以及其他选项卡之一中的 ViewController。
据我了解,ViewController 的 viewDidLoad 方法仅在首次选择该选项卡时调用。有没有办法强制 TabBarController 调用 ViewController viewDidLoad 方法,即使它不活动?
谢谢
I have a TabBarController with 7 Custom ViewControllers and what i am trying to do is have the TabBarController on startup load the first ViewController in its array as well as a ViewController in one of the other tabs.
As far as i understand it the viewDidLoad method for the ViewController's is only called for that tab when it is first selected. Is there a way to force the TabBarController to call a ViewController viewDidLoad method even if its not active?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需引用 ViewController 视图:
如果 myViewController 的视图为零,那么它将被加载。
请注意,即使这种方法有效,您的应用程序也会受到视图加载/卸载机制的影响,该机制由内部运行循环逻辑控制,而不是由您的应用程序控制,而视图控制器“内部逻辑”应由 initWithNib 初始化:完全由您控制的方法。但这只是一个避免奇怪错误的建议,无论如何提出的解决方案是有效的。
Just reference the ViewController view:
If myViewController's view is nil, then it will be loaded.
Note that even if this approach is working, your app will be affected by the view loading/unloading mechanism which is controlled by the internal run loop logic and not by your app, while the view controller "internal logic" should be initialized by the initWithNib: method which is completely under your control. But this is just a suggestion to avoid strange bugs, anyway the solution proposed is working.
这个技术看起来效果相当不错。我有一个应用程序,底部有一个 UITabBarController,里面有 5 个按钮。当用户单击第三个按钮时,该视图的 viewDidLoad 需要 5 秒来执行操作,因此我使用此技术来使其 viewDidLoad 在应用程序启动时被调用。我不想显示第三个视图;只是要初始化,以便单击时立即显示。
我必须将 UITabBarController 子类化为 FoohBarController 之类的子类,然后在它的 viewDidLoad 中我创建了一个后台线程来执行此操作:
This technique seems to work pretty good. I have an app with a UITabBarController at the bottom with 5 buttons in it. When the user clicks the 3rd button, the viewDidLoad for that view took 5 seconds to do stuff so I used this technique to cause its viewDidLoad to get called when the app starts. I don't want the 3rd view to be displayed; just to be initialized so it shows up instantly when clicked.
I had to subclass the UITabBarController to something like FoohBarController, then in it's viewDidLoad I made a background thread do this: