Directly on the code:
////APPDELEGATE.M////#import "AppDelegate.h" #import "RootViewController.h" #import "FirstViewController.h" #import "SecnodViewController.h" #import "ThirdViewController.h" @interface appdelegate() <uitabbarcontrollerdelegate>@property(nonatomic,Assign)NsintegerPreviousindex;//Previous selected subscript@end @implementation appdelegate - (void) Dealloc {[_window release]; [SuperDealloc];} - (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) Launchoptions { Self. Window= [[[UIWindowAlloc] Initwithframe:[[uiscreen Mainscreen] bounds]] autorelease];//Override point for customization after application launch. Self. Window. BackgroundColor= [UicolorWhitecolor]; [ Self. WindowMakekeyandvisible]; [[Uitabbar appearance] setbartintcolor:[UicolorLightgraycolor]]; [[UinavigationbarAppearance] Setbartintcolor:[UicolorBluecolor]];//Rootviewcontroller *ROOTVC = [[Rootviewcontroller alloc] init];//Uinavigationcontroller *navi = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:ROOTVC]; //Create a label view Controller Uitabbarcontroller*tabbarcontroller = [[UitabbarcontrollerALLOC] init];//How do I manage the view controller? Nsarray*classnames = @[@"Firstviewcontroller", @"Secnodviewcontroller", @"Thirdviewcontroller", @"Thirdviewcontroller", @"Thirdviewcontroller"];Nsmutablearray*viewcontroller = [NsmutablearrayArray]; for(inti =0; I < Classnames. Count; i++) {Uiviewcontroller*aviewcontroller = [[Nsclassfromstring (Classnames[i]) alloc] init];/// Specify a label for the currently created view controller, and the view controller that will be managed by the tag controller needs to provide it with a Tabbaritem object to display the corresponding label on the tab bar. //Tag controller management of the amount of view controller once the number of more than five, it will automatically generate a more label corresponding to the list view controller management of Redundant view Controller, general project development has a tag view controller management of the view controller is not designed to more than five cases, most of the concentration of 4 View Controller. //Aviewcontroller.tabbaritem = [[[Uitabbaritem Alloc] initwithtabbarsystemitem:arc4random ()% tag:100 + i] Autorelease]; UIImage*image = [[UIImageimagenamed:[NSStringstringwithformat:@"icon0%d", i +1]] [imagewithrenderingmode:uiimagerenderingmodealwaysoriginal];UIImage*selectedimage = [[UIImageimagenamed:[NSStringstringwithformat:@"icon0%d_s", i +1]] [imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; Aviewcontroller. Tabbaritem= [[[[Uitabbaritem alloc] Initwithtitle:NilImage:image Selectedimage:selectedimage] autorelease];//When the picture position size is not appropriate, you can use the Imageinsets property of Tabbaritem to adjust its size and position. Aviewcontroller. Tabbaritem. Imageinsets= Uiedgeinsetsmake (6, -5, -6,5);//Aviewcontroller.title = [NSString stringwithformat:@ "%d", i + 1]; //display of ①②③④⑤ styleAviewcontroller. Tabbaritem. Badgevalue= [NSStringstringwithformat:@"%d", i +1]; [Viewcontroller Addobject:aviewcontroller]; } Tabbarcontroller. Viewcontrollers= Viewcontroller;//// Set the Tintcolor and Bartintcolor of the tab bar//TabBarController.tabBar.tintColor = [Uicolor greencolor];///////////////////// Some system-style label highlighting depends on the Tintcolor //TabBarController.tabBar.barTintColor = [Uicolor redcolor]; //Set label subscript for default selectionTabbarcontroller. SelectedIndex=2; Self. Previousindex= Tabbarcontroller. SelectedIndex;//Specify proxy object for label ControllerTabbarcontroller. Delegate= Self; Self. Window. Rootviewcontroller= Tabbarcontroller; [Tabbarcontroller release];//tabbarcontroller.viewcontrollers = @[navi];//Self.window.rootViewController = Tabbarcontroller;// //[ROOTVC release];//[Navi release];//[Tabbarcontroller release]; return YES;}//whether to allow switching to the selected view Controller- (BOOL) Tabbarcontroller: (Uitabbarcontroller*) Tabbarcontroller Shouldselectviewcontroller: (Uiviewcontroller*) Viewcontroller {NSLog( @'%s ', __function__);//When clicking on the label Switch View Controller, the label controller will use this protocol method to determine if it is allowed to switch to the corresponding controller, and if it returns no, the view controller cannot be switched. 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 on-move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. //Use this method to the 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 the restore your application to the 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 M Ade on entering the background.}- (void) Applicationdidbecomeactive: (uiapplication*) Application {//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.}- (void) Applicationwillterminate: (uiapplication*) Application {//Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.}@end //Appdelegate
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Uitabbarcontroller----Tag View Controller