UITabBarController ---- 標籤視圖控制器

來源:互聯網
上載者:User

標籤:視圖控制器   import   interface   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 ---- 標籤視圖控制器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.