问题描述
我有一个标签栏应用程序.在一个标签下,我想要顶部导航视图中的uisegmentedControl,该控件控制当前显示的视图.如果我只是交换视图,这是非常容易的,但是我想以一种更有组织性和更通用的方式来做到这一点,即为每个视图使用一个uiviewcontroller并以最优化的方式交换它们.
I have a tab bar app. Under one of the tabs i want a uisegmentedControl in the top navigation view, that controls what view is currently displayed. This is dead easy if i just exchange the view, but i want to do it in a more organized and generic way, by using one uiviewcontroller for each view and exchanging them in the most optimzed way.
我想第一步是确切地知道在更改选项卡后,选项卡控制器会发送给导航控制器/视图控制器什么,然后从那里进行计算.
i guess step one would be to know exactly what a tabbar controller sends to a navigation controller/view controller when a tab is changed, and work it out from there.
有人能指出我正确的方向吗?
Can any one point me in the right direction?
推荐答案
前段时间,我偶然发现了SegmentsController
,它在此博客来自红色工匠的条目.
我将它与UITabBarController结合使用,但不知道我做错了什么.没错,例如它崩溃"或它没有执行我想要的操作",但在某种意义上说是错误的,因为我必须将每个UIViewController调用(例如viewDidAppear,receivedMemoryWarning等)转发给子级viewControllers.代码错误的应用仍在应用商店中,而我从未收到有关它的投诉.
Some time ago I stumbled upon SegmentsController
which I found in this blog entry from red artisan.
I used it in conjunction with a UITabBarController, but without knowing I did it wrong. Not wrong as in "it crashs" or "it doesn't do what i want" but wrong in the sense that I have to forward each UIViewController call (like viewDidAppear, receivedMemoryWarning etc) to the child viewControllers. The app with the wrong code is still in the app store and I never received a complain about it.
但是我玩了一段时间,并弄清楚了如何正确使用它.这有点麻烦,但是恕我直言,这绝对值得.我将向您展示我现在拥有的正确版本,因为我正在Interface Builder中创建UITabBarController,所以我必须更改代码中的选项卡.这带来了另一麻烦,也许还有改进的余地.但是现在我对这种解决方案感到满意.
But I played around a while and figured out how to use it right. It's a bit of a hassle but imho it's absolutely worth it.I'll show you the correct version that I have right now, I'm creating the UITabBarController in Interface Builder so I have to change the tab in code. Which introduces another piece of mess, and maybe there is room for improvements. But right now I'm satisfied with this solution.
NSMutableArray *items = [self.tabBarController.viewControllers mutableCopy]; // tabs from tabbar configured in IB
// The two child vc that will appear in the segment control
SomeViewController_iPhone *tvcs = [[[SomeViewController_iPhone alloc] initWithNibName:@"SomeView_iPhone" bundle:nil] autorelease];
SomeOtherViewController_iPhone *tvct = [[[SomeOtherViewController_iPhone alloc] initWithNibName:@"SomeOtherView_iPhone" bundle:nil] autorelease];
NSArray *viewControllers1 = [NSArray arrayWithObjects:tvcs, tvct, nil];
// the nav controller acts as a wrapper around the child viewcontrollers
UINavigationController *navController1 = [[[UINavigationController alloc] init] autorelease];
navController1.tabBarItem.title = NSLocalizedString(@"FirstTab", nil);
navController1.tabBarItem.image = [UIImage imageNamed:@"tabImage1.png"];
navController1.navigationBar.tintColor = [UIColor navBarTintColor];
firstTabSegmentsController = [[SegmentsController alloc] initWithNavigationController:navController1 viewControllers:viewControllers1];
// uses a NSArray category that basically creates a NSArray that has the title properties of the vc in viewControllers1
firstTabSegmentedController = [[UISegmentedControl alloc] initWithItems:[viewControllers1 arrayByPerformingSelector:@selector(title)]];
firstTabSegmentedController.frame = CGRectMake(0, 0, 222, 30);
firstTabSegmentedController.segmentedControlStyle = UISegmentedControlStyleBar;
firstTabSegmentedController.selectedSegmentIndex = 0;
[firstTabSegmentsController indexDidChangeForSegmentedControl:firstTabSegmentedController];
[firstTabSegmentedController addTarget:firstTabSegmentsController action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// replace first tab from interface builder with this
[items replaceObjectAtIndex:0 withObject:navController1];
如您所见,它需要一些设置,但是在我看来,该解决方案比我一直尝试的其他解决方案都要好.我希望我能正确删除NDA代码.
as you see it needs a bit of setup, but in my opinion this solution is better than anything else I've tried throughout the time. I hope I de-NDAed the code correctly.
上传了一个示例项目: BeautifulColors.zip
Uploaded a sample project: BeautifulColors.zip
这篇关于实现我自己的导航控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!