2013-03-05 87 views
-1

我實現了一個自定義選項卡欄控制器作爲一組按鈕,每個按鈕都與它自己的View Controller相關。我在此鏈接上指導http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/以實現此行爲。所以代碼的相關部分如下:自定義TabBar導航問題

- (void) selectedItemAtIndex:(NSUInteger)itemIndex 
{ 
// Get the right view controller 
NSDictionary* data = [self.tabBarItems objectAtIndex:itemIndex]; 
UIViewController* viewController = [data objectForKey:@"viewController"]; 

// Remove the current view controller's view 
UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG]; 
[currentView removeFromSuperview]; 


// Set the view controller's frame to account for the tab bar (+ 48) 
viewController.view.frame = CGRectMake(0,48,self.view.bounds.size.width, self.view.bounds.size.height - 48); 

// Se the tag so we can find it later 
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG; 

// Add the new view controller's view 
[self.view insertSubview:viewController.view belowSubview:self.tabBar]; 

//Keep track of current view controller 
self.currentController = viewController; 
} 

到目前爲止是工作,我可以用類似的滿耳的默認TabBarViewController看到每個視圖控制器。但是有一個要求,我需要從一個tabBar控制器內部模態地推送一個新的導航控制器(它應該採用所有的應用程序框架)。

乍一看我從標籤控制器中的一個內嘗試了以下的代碼:

DetailViewController *detailViewController = [[DetailViewController alloc]init]; 
UINavigationController *navigationController = [[UINavigationController alloc]detailViewController]; 
[self presentModalViewController:navigationController animated:YES]; 

但是不工作如預期,第一視圖中示出了的TabBar下面和第二新的視圖沒有采取考慮到應該是屏幕的父視圖框架限制少了tabbar。 (0,48,360,412)。我的詳細視圖控制器是從nib文件加載內容。

那麼,這是很明顯的,因爲TabBar控制器插入我的自定義TabBar下面的每個視圖。

[self presentModalViewController:navigationController animated:YES]; 

所以,我想直接將它作爲一個窗口子視圖:

[[UIApplication sharedApplication].keyWindow addSubview:navigationController.view]; 

但是,我認爲這是不行的...應該有一個更好的方法,我想不通。所以如果任何人都可以給我一些關於如何糾正或改善這個導航系統的建議,那將會很棒。

非常感謝。

回答

0

如果您正在構建iOS 5.0及更高版本的應用程序,則可以使用childViewController。在您的自定義選項卡欄中,您可以有一個containerView和一個tabView。

viewController的視圖被添加到containerView。所有必要的事件產生到後來加入的viewController如果下面的方法是否正確

- (void)addChildViewController:(UIViewController *)childController; 
- (void)removeFromParentViewController; 

更多關於實施遏制的viewController可以發現here.

+0

謝謝,不幸的是我需要支持4.x的設備。 – Pablo 2013-03-05 16:01:55

+0

如果您有權作出決定,您可以選擇5.0的最低目標,因爲這會使視圖控制器的遏制非常容易。 – Anupdas 2013-03-05 16:12:27