【发布时间】:2015-02-07 14:43:33
【问题描述】:
我正在设计一个应用程序并希望使用 UITabBarController 来管理我的视图控制器。但是我有超过 5 个视图控制器要显示,但我不喜欢苹果默认的“moreNavigationController”设计来显示额外的视图控制器。我宁愿让第 5 个选项卡像一个开关一样改变其他选项卡和链接的视图控制器。我认为这不是苹果对 Tab Bar Controller 的首选用法。
我找到了一种方法,它似乎可以正常工作而不会出错。但想知道这是否是个好主意。我没有看到使用这种方法的其他示例。任何想法将不胜感激。谢谢。这是精简/伪代码:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController* mainTabController = [[UITabBarController alloc] init];
// Make the appDelegate the TabBarController delegate to process method below
mainTabController.delegate = self;
// Add my custom View Controllers as first four objects
// Add a blank View Controller as the fifth object which will act as a switch
self.window.rootViewController = mainTabController;
[self.window addSubview:mainTabController.view];
[self.window makeKeyAndVisible];
return YES;
}
// TabBarController delegate method
// Called whenever a tab is selected
-(BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// Test for the 5th View Controller Tab selection
if (viewController == matches the fifth VC)
{
// Add a new set of VC to an array again with the fifth being a blank
// which is never selected and acts as a switch
[tabBarController setViewControllers:array animated:YES];
// return No to ensure the 5th tab VC is not shown
return NO;
}
// Again Test for the new 5th VC added above
if (viewController == matches the new fifth VC)
{
// Same setup as above but selecting the original view controllers array
[tabBarController setViewControllers:array animated:YES];
return NO;
}
return YES;
}
【问题讨论】:
标签: ios tabs uitabbarcontroller uitabbar