以下代码实现各个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实现界面的切换