Knowledge points:
1.UITabBarController uses
2.UITabBarItem
As for TabBarController, in addition to the content of this collation, I would like to read these two articles which are very useful in practical development.
The Basic Use of RDVTabBarController and the Method to Prevent Double Click of tabbar
Jump from the controller on one item of tabBarController to the controller on another item
=======================
UITabBarController
1. Creation method
2. How to add a UIViewController to UITabBarController
3. How to add a UINavigation Controller to UITabBarController
4. How to remember the order of clicks
5. Setting and obtaining the selected item
@property(nonatomic) NSUInteger selectedIndex;
=======================
UITabBarItem
1. The relationship between UITabBarItem and UITabBarController
tabBarItem is an attribute of UIViewController
This property is for UITabBarController
2. Creating UITabBarItem for Text and Image
- (id)initWithTitle:(NSString *)title
image:(UIImage *)image
tag:(NSInteger)tag;
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { //Setting the type of text band picture self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"gules" image:[UIImage imageNamed:@"1_selected"] tag:0]; //Set text, select pictures and unchecked picture types // self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"gules" image:[UIImage imageNamed:@"3_normal"] selectedImage:[UIImage imageNamed:@"3_selected"]]; }
Note: Functions added after ios7
- (instancetype)initWithTitle:(NSString *)title
image:(UIImage *)image
selectedImage:(UIImage *)selectedImage
3. Create UITabBar System Item that comes with the system
- (id)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem
tag:(NSInteger)tag;
//Setting System Type self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
4. Setting UITabBarItem logo
@property(nonatomic,copy) NSString *badgeValue
//Set logo self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",10L];
=======================
Notes for UITabBarController
1.UITabBarController Quantity Limitation
1) Only five attempt controllers are allowed to be displayed in tabBarController
A more is automatically added to the system that exceeds it.
2.UITabBarController Editor
The user can set the display position of each control at will.
=======================
UITabBarController order
1. Record the last click of viewController by the user
1) NSUser Defaults role
NSUserDefaults objects are used to save, restore application-related preferences, configure data, and so on.
2) NSUser Defaults creation
+ (NSUserDefaults *)standardUserDefaults
3) Storage of data
- (void)set [data type]: (data type) value for Key: (NSString *) defaultName;
4) Synchronization of data
- (BOOL)synchronize;
5) Reading data
- (Data Type) [Data Type] ForKey:(NSString*) defaultName;
2. UITabBarController Delegate
1) When selected
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
#pragma mark- UITabBarControllerDelegate //A controller has been selected -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ NSLog(@"The controller currently in the selected state is subscribed to%ld",tabBarController.selectedIndex); //Record NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; [def setInteger:tabBarController.selectedIndex forKey:@"index"]; //Synchronize to local [def synchronize]; }
2) Control whether TabBarItem can be selected
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController;
//Prepare to select a controller (whether or not the controller can be selected) -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ //viewController Controller Objects to be Selected //tabBarController Column Controller for Current Management //NSLog(@"The controller currently in the selected state is subscribed to%ld",tabBarController.selectedIndex); /*Wrong Writing: The first controller is not allowed to be selected if (tabBarController.selectedIndex == 0) { return NO; } */ if (viewController == tabBarController.viewControllers[0]) { return NO; } //Return YES,Can be selected,Return NO,No selection allowed return YES; }
3) The following three methods are mainly used to monitor the edit operation of view controller in more ViewController
#pragma mark- UITabBarControllerDelegate 1.//Ready to start editing -(void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{ NSLog(@"willBeginCustomizingViewControllers"); } 2.//Ready to End Editing -(void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{ NSLog(@"willEndCustomizingViewControllers"); } 3.//Ended editing -(void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{ //Judge if the order has changed if (changed) { //Record Title Order NSMutableArray *newArr = [NSMutableArray new]; for (UIViewController *ctl in viewControllers) { [newArr addObject:ctl.title]; } //Save it locally NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; [def setObject:newArr forKey:@"saveArr"]; [def synchronize]; } }