开发者

Problems with UINavigation controller inside of UITabbarcontroller

I have an开发者_StackOverflow中文版 UITabbarcontroller with 4 tabbar items.Each tabbar items have UINavigationController.Actually Tabbar loads the last view controller from stack while i switch next tabbar item.

I want to load the first view controller from the stack whenever i switch between the tabbar item.

How can i achieve this?


You want it to go back to the root view controller within each tab, rather than remembering where in the hierarchy the user was, whenever you switch tabs?

You need to call popToRootViewControllerAnimated:NO on the navigation controller as you switch tabs (i.e. in a tab bar controller delegate method).


The best approach you may follow is :

Appdelegate.h

UITabBarController *tabBarController;
// set properties

Appdelegate.m

// Synthsize

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;

//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController  
Search * search = [[Search alloc] init];  
UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:search];  

Nearby* nearby = [[Nearby alloc] init];  
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];  

Map* map = [[Map alloc] init];  
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];  

AboutUs* aboutUs = [[AboutUs alloc] init];  
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];  

Favorites* favorites = [[Favorites alloc] init];  
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];  

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];  

tabBarController.viewControllers = controllers;

[window addSubview:tabBarController.view];    

You can accordingly manage in which tab you want to place navigation controller or only a view controller.
Then in each of the view controllers mentioned above you need to implement

- (id)init {}  

in which you can set Tab name and image.
Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜