UITabBarController
區別
UIToolBarController 用於實現平級導航,比如QQ 微信下邊的三個按鈕 實現平級視圖的切換
UINavigationController 用於實現具有層級關係的導航實現
使用方法
FirstViewController * fVC = [[FirstViewController alloc]init];
UINavigationController * uNC1 = [[UINavigationController alloc]initWithRootViewController:fVC];
SecendViewController * sVC = [[SecendViewController alloc]init];
UINavigationController * uNC2 = [[UINavigationController alloc]initWithRootViewController:sVC];
ThirdViewController *tVC = [[ThirdViewController alloc]init];
UINavigationController * uNC3 = [[UINavigationController alloc]initWithRootViewController:tVC];
FourthViewController * forVC = [[FourthViewController alloc]init];
UINavigationController * uNC4 = [[UINavigationController alloc]initWithRootViewController:forVC];
UITabBarController * uTBC = [[UITabBarController alloc]init];
uTBC.viewControllers = @[uNC1,uNC2,uNC3,uNC4];
相關屬性
tVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"find" image:[UIImage imageNamed:@"home_tabbar_icon_find.png"] selectedImage:[UIImage imageNamed:@"home_tabbar_icon_find_sel.png"]];
tVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",10];
uTBC.tabBar.barStyle = UIBarStyleBlack;
uTBC.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bar.png"];
uTBC.selectedIndex = 1;
NSArray * array = uTBC.viewControllers;
代理方法
uTBC.delegate = self;
-(void)tabBarController:(nonnull UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController
-(BOOL)tabBarController:(nonnull UITabBarController *)tabBarController shouldSelectViewController:(nonnull UIViewController *)viewController
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
NSUserDefaults
[[NSUserDefaults standardUserDefaults] setValue:@0 forKey:@"last"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[[NSUserDefaults standardUserDefaults] objectForKey:@"last"] integerValue];