2012-12-14 77 views
1

我有一個加載UITabBarController的類。每個選項卡打開一個UINavigationController。從UITabBar預加載視圖

我想在我的UINavigationControllers中預加載視圖。我試過這樣做:

UITabBarController * tabBarController = (UITabBarController *)self.centerController; 
NSArray *myViewControllers = tabBarController.viewControllers; 
for (UINavigationController *navViewController in myViewControllers) 
{ 
    [navViewController loadView]; 
} 

我嘗試過不同的事情,但它永遠不會被加載。難道我做錯了什麼?

回答

9

感謝或者阿貝爾,你竟然幫我理解這個問題。我必須在UINavigationController中的第一個UIViewController上調用view。以下是可用的代碼:

UITabBarController * tabBarController = (UITabBarController *)self.centerController; 
NSArray *myViewControllers = tabBarController.viewControllers; 
for (UINavigationController *navViewController in myViewControllers) 
{ 
    [[navViewController.viewControllers objectAtIndex:0] view]; 
} 
+1

很高興我能幫上忙 –

1

嘗試

UITabBarController * tabBarController = (UITabBarController *)self.centerController; 
NSArray *myViewControllers = tabBarController.viewControllers; 
for (UINavigationController *navViewController in myViewControllers) 
{ 
    [navViewController view]; 
} 
+0

沒有運氣。我認爲這個問題可能是因爲我有一個奇怪的結構:我的UIViewController嵌入了一個嵌入了UIViewController的UINavigationController的UITabBarController。 我試圖加載他們每個人的看法,但它仍然無法正常工作。 – syrio

2

如果您使用自動佈局,則必須告訴視圖以相應地佈置其子視圖。

試試這個

UITabBarController * tabBarController = (UITabBarController *)self.centerController; 
NSArray *myViewControllers = tabBarController.viewControllers; 
for (UINavigationController *navViewController in myViewControllers) { 
    UIViewController *ctrl = navViewController.topViewController; 
    [ctrl.view setNeedsLayout]; 
    [ctrl.view layoutIfNeeded]; 
}