天天看點

如何通過視圖控制器UITabBarController實作界面的切換

以下代碼實作各個View之間的切換。

建立UITabBarController,

建立視圖控制器UIViewControlle

定義控制器的UITabBarItem

将UIViewController數組加入UITabBarController.viewControllers

将UITabBarController設定為視窗的根控制器

為友善看寫到了一個檔案,要編寫每個頁面的功能,可以建立繼承于UIViewControlle的子類,并在它的init方法裡實作一些想要實作的功能。

UITabBarController預設隻能顯示5個,當多于5個系統會自動将第五個變為....,點第5個裡面就能看到未顯示的item

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    UITabBarController * tabVC = [[UITabBarController alloc]init];

    // 1.建立視圖控制器

    UIViewController * vc1 = [[UIViewController alloc]init];

    vc1.view.backgroundColor = [UIColor redColor];

    UIViewController * vc2 = [[UIViewController alloc]init];

    vc2.view.backgroundColor = [UIColor greenColor];

    UIViewController * vc3 = [[UIViewController alloc]init];

    vc3.view.backgroundColor = [UIColor yellowColor];

    UIViewController * vc4 = [[UIViewController alloc]init];

    vc4.view.backgroundColor = [UIColor blueColor];

    UIViewController * vc5 = [[UIViewController alloc]init];

    vc4.view.backgroundColor = [UIColor grayColor];

    UIViewController * vc6 = [[UIViewController alloc]init];

    vc4.view.backgroundColor = [UIColor lightGrayColor];

    // 2.定義item

    UITabBarItem * item1 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:100];

    UITabBarItem * item2 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:101];

    UITabBarItem * item3 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:102];

    UITabBarItem * item4 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:103];

    UITabBarItem * item5 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearchtag:104];

    UITabBarItem * item6 = [[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:105];

    vc1.tabBarItem = item1;

    vc2.tabBarItem = item2;

    vc3.tabBarItem = item3;

    vc4.tabBarItem = item4;

    vc5.tabBarItem = item5;

    vc6.tabBarItem = item6;

    // 3.将視圖控制器指派給tabVC

    tabVC.viewControllers = @[vc1,vc2,vc3,vc4,vc5,vc6];

    self.window.rootViewController = tabVC;

    return YES;

}

- (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

如何通過視圖控制器UIViewController實作界面的切換

如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換如何通過視圖控制器UITabBarController實作界面的切換