UITabBarController ---- 标签视图控制器

Posted mfmdaoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITabBarController ---- 标签视图控制器相关的知识,希望对你有一定的参考价值。

直接上代码:

//
//  AppDelegate.m
//
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "FirstViewController.h"
#import "SecnodViewController.h"
#import "ThirdViewController.h"


@interface AppDelegate()<UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger previousIndex ; // 上一个选中的下标

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


    [[UITabBar appearance] setBarTintColor:[UIColor lightGrayColor]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];


//    RootViewController *rootVC = [[RootViewController alloc] init];
//    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];

    // 创建标签视图控制器
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    //怎样管理视图控制器?
    NSArray *classNames = @[@"FirstViewController", @"SecnodViewController", @"ThirdViewController", @"ThirdViewController", @"ThirdViewController"];
    NSMutableArray *viewController = [NSMutableArray array];
    for (int i = 0; i < classNames.count; i++) {
        UIViewController *aViewController = [[NSClassFromString(classNames[i]) alloc] init];
        /// 为当前创建的视图控制器指定标签,将要被标签控制器所管理的视图控制器须要为其提供 tabBarItem 对象才可在标签栏上显示相应的标签。
        // 标签控制器管理的额视图控制器一旦数量超过五个,就会自己主动生成一个 more 标签相应的列表视图控制器管理多余的视图控制器,一般项目开发中有标签视图控制器管理的视图控制器也不会设计为超过五个的情况,大多数集中为 4 个视图控制器。

// aViewController.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:arc4random() % 12 tag:100 + i] autorelease]; UIImage *image = [[UIImage imageNamed:[NSString stringWithFormat:@"icon0%d", i + 1]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIImage *selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"icon0%d_s", i + 1]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; aViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:nil image:image selectedImage:selectedImage] autorelease]; //当图片位置大小不合适时,能够使用 tabBarItem 的 imageInsets 属性来调整其大小和位置。 aViewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6 , -5, -6, 5); // aViewController.title = [NSString stringWithFormat:@"第%d个", i + 1]; // 显示 ① ② ③ ④ ⑤ 样式的 aViewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", i + 1]; [viewController addObject:aViewController]; } tabBarController.viewControllers = viewController; // // 设置 标签栏的 tintColor 和 barTintColor // tabBarController.tabBar.tintColor = [UIColor greenColor]; // // 一些系统样式的标签高亮颜色会依赖标签栏的 tintColor 属性。 // tabBarController.tabBar.barTintColor = [UIColor redColor]; // 设置默认 选中的 标签下标 tabBarController.selectedIndex = 2; self.previousIndex = tabBarController.selectedIndex; //指定标签控制器的代理对象 tabBarController.delegate = self; self.window.rootViewController = tabBarController; [tabBarController release]; // tabBarController.viewControllers = @[navi]; // self.window.rootViewController = tabBarController; // // [rootVC release]; // [navi release]; // [tabBarController release]; return YES; } // 是否同意切换到选中的视图控制器 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { NSLog( @"%s", __FUNCTION__ ) ; // 当点击标签切换视图控制器时标签控制器会通过此协议方法来确定是否同意切换到相应控制器。假设返回为NO,则不能切换视图控制器。 return YES; } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog( @"%s", __FUNCTION__ ) ; // viewController.tabBarItem.badgeValue = nil; if (self.previousIndex != tabBarController.selectedIndex) { UIViewController *aViewController = tabBarController.viewControllers[self.previousIndex]; aViewController.tabBarItem.badgeValue = nil; self.previousIndex = tabBarController.selectedIndex; } } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end // AppDelegate

以上是关于UITabBarController ---- 标签视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

从一个 UITabBarController 导航到另一个 UITabBarController

UITabBarController

如何使屏幕仅垂直。 (UITabBarController/Swift)

UITabBarController简单介绍

UITabBarController

UITabBarController 上的 WYPopoverController 下降