今天想实现一个功能,就是左侧是导航栏(比如Search by name, Search by color),点击后左侧显示不同的界面(有导航栏),左侧的界面都有tableview(所有的name or color),点击后切换到详细列表(根据具体的名字索引or颜色索引搜到的具体物品列表)。我左侧的导航栏用的是tableview,就像我点击不同cell后面,左侧界面不但可以切换,而且再次点击回去后,上次还是显示上次的界面。比如进来是选中Search by name,左侧是a,b,c,d....然后我点了a显示以a开头的物品,然后我点击左侧导航栏Search by color,这是左侧应该显示一个新的界面red,green。。。这是我再点击左侧导航栏Search by name,应该是回到刚才我看到的以a开头的物品列表界面。其实左侧导航栏就类似tab bar的功能。最终想到Search by name, Search by color对应的是两个栈:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
navSearchByNameViewController = [[UINavigationController alloc] initWithRootViewController:[SearchByNameViewController new]];
navSearchByColorViewController = [[UINavigationController alloc] initWithRootViewController:[SearchByColorViewController new]];
navSearchByStateViewController = [[UINavigationController alloc] initWithRootViewController:[SearchByStateViewController new]];
navMoreInfoViewController = [[UINavigationController alloc] initWithRootViewController:[NIMMoreInfoViewController new]];
controllerArray = [NSArray arrayWithObjects:navSearchByNameViewController, navSearchByColorViewController, navSearchByStateViewController, navMoreInfoViewController, nil];
selectedVCIndex = -1;
[self selectViewControllerAtIndex:0];//默认进入主界面显示第一个vc
}
- (void) selectViewControllerAtIndex:(int)index {
if (selectedVCIndex != -1) {
[[(UIViewController *)[controllerArray objectAtIndex:selectedVCIndex] view] removeFromSuperview];
}
selectedVCIndex = index;
[self.view addSubview:[(UIViewController *)[controllerArray objectAtIndex:index] view]];
//[self.view setNeedsLayout];
}
另外replace the root view controller instead of pushing the view controller onto the navigation stack:
[self.navigationController setViewControllers:[NSArray arrayWithObject:theNewViewController] animated:YES];
http://stackoverflow.com/questions/13205100/using-uinavigationcontroller-view
http://stackoverflow.com/questions/5272371/how-does-uitabbarcontroller-work