改变 UITabBarController 的行为
Changing the behaviour of UITabBarController
我正在设计一个应用程序并想使用 UITabBarController 来管理我的视图控制器。但是我有超过 5 个视图控制器要显示,但我不喜欢苹果默认的 "moreNavigationController" 设计来显示额外的视图控制器。我更愿意让第 5 个选项卡像一个开关一样改变其他选项卡并链接视图控制器。我想这不是苹果对 Tab Bar Controller 的首选用法。
我找到了一种方法,它似乎工作正常,没有错误。但想知道这是否是个好主意。我还没有看到使用这种方法的其他例子。任何想法将不胜感激。谢谢。这是剥离的 down/pseudo 代码:-
- (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;
}
我发现将 Apple 的 API 用于不是首选用途的东西总是一个坏主意,这是出于多种原因:
对于 Apple 进行的每次更新,您都需要检查您的代码,看它是否没有破坏它。
您现在可能找到了一种方法,但是一旦您想要做更多事情,就会遇到问题。
我的建议是,创建你自己的 TabBarController,它并不难制作,你可以让它适合你的想法。
我正在设计一个应用程序并想使用 UITabBarController 来管理我的视图控制器。但是我有超过 5 个视图控制器要显示,但我不喜欢苹果默认的 "moreNavigationController" 设计来显示额外的视图控制器。我更愿意让第 5 个选项卡像一个开关一样改变其他选项卡并链接视图控制器。我想这不是苹果对 Tab Bar Controller 的首选用法。
我找到了一种方法,它似乎工作正常,没有错误。但想知道这是否是个好主意。我还没有看到使用这种方法的其他例子。任何想法将不胜感激。谢谢。这是剥离的 down/pseudo 代码:-
- (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;
}
我发现将 Apple 的 API 用于不是首选用途的东西总是一个坏主意,这是出于多种原因:
对于 Apple 进行的每次更新,您都需要检查您的代码,看它是否没有破坏它。 您现在可能找到了一种方法,但是一旦您想要做更多事情,就会遇到问题。
我的建议是,创建你自己的 TabBarController,它并不难制作,你可以让它适合你的想法。