使用TabBarController时如何加载多个ViewController?

发布于 2024-10-16 02:44:01 字数 271 浏览 0 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-10-23 02:44:01

只需引用 ViewController 视图:

[myViewController view]

如果 myViewController 的视图为零,那么它将被加载。

请注意,即使这种方法有效,您的应用程序也会受到视图加载/卸载机制的影响,该机制由内部运行循环逻辑控制,而不是由您的应用程序控制,而视图控制器“内部逻辑”应由 initWithNib 初始化:完全由您控制的方法。但这只是一个避免奇怪错误的建议,无论如何提出的解决方案是有效的。

Just reference the ViewController view:

[myViewController 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.

失退 2024-10-23 02:44:01

这个技术看起来效果相当不错。我有一个应用程序,底部有一个 UITabBarController,里面有 5 个按钮。当用户单击第三个按钮时,该视图的 viewDidLoad 需要 5 秒来执行操作,因此我使用此技术来使其 viewDidLoad 在应用程序启动时被调用。我不想显示第三个视图;只是要初始化,以便单击时立即显示。

我必须将 UITabBarController 子类化为 FoohBarController 之类的子类,然后在它的 viewDidLoad 中我创建了一个后台线程来执行此操作:

{
  // get a pointer to the 3rd item in the tab bar (0 based)
  UINavigationController *navcon = [self.viewControllers objectAtIndex:2]; 

  // get a pointer to the viewController I want to init
  CalendarViewController *calendar = [navcon.viewControllers objectAtIndex:0];

  // Just ask for the view. This will force the view to load and to initialize
  UIView *v = calendar.view;
  v = nil;    // to remove a compiler warning
}

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:

{
  // get a pointer to the 3rd item in the tab bar (0 based)
  UINavigationController *navcon = [self.viewControllers objectAtIndex:2]; 

  // get a pointer to the viewController I want to init
  CalendarViewController *calendar = [navcon.viewControllers objectAtIndex:0];

  // Just ask for the view. This will force the view to load and to initialize
  UIView *v = calendar.view;
  v = nil;    // to remove a compiler warning
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文