【问题标题】:Changing the behaviour of UITabBarController改变 UITabBarController 的行为
【发布时间】: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


    【解决方案1】:

    我发现将 Apple 的 API 用于非首选用途总是一个坏主意,这是出于多重原因:

    您需要针对 Apple 进行的每次更新检查您的代码,看看它是否不会破坏它。 您现在可能会找到方法,但是一旦您想做更多事情,就会遇到问题。

    我的建议是,创建您自己的 TabBarController,制作起来并不难,您可以根据自己的想法进行调整。

    【讨论】:

    • 是的,我脑海中闪过这个想法。您将如何创建 TabBarController 是否有任何示例代码或指南?
    • 这个项目我用过一次,对你有帮助:github.com/mhaddl/MHCustomTabBarController
    • 感谢您的帮助。决定尝试创建一个像控制器这样的自定义标签栏。阅读了苹果 viewController 指南。我从来没有意识到他们可以这样设置。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多