开发者

How to load more than one ViewController when using TabBarController?

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开发者_如何学JAVA?

thx


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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜