UITabBarController

UITabBarController

1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2. // Override point for customization after application launch.
3. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
4. self.window.backgroundColor = [UIColor whiteColor];
5. [self.window makeKeyAndVisible];
6.
7. ViewController *rootController = [[ViewController alloc] init];
8. SecondViewController *second = [[SecondViewController alloc] init];
9.
10. UITabBarController *tabBarController = [[UITabBarController alloc] init];
11. [tabBarController addChildViewController:rootController];
12. [tabBarController addChildViewController:second];
13.
14.
15. UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:nil
16. image:[UIImage imageNamed:@"tabbar_sound_n"]
17. selectedImage:[UIImage imageNamed:@"tabbar_sound_h"]];
18.
19. rootController.tabBarItem = item;
20. rootController.tabBarItem.badgeValue = @"123";
21.
22. UITabBarItem *secondItem = [[UITabBarItem alloc] initWithTitle:nil
23. image:[UIImage imageNamed:@"tabbar_me_n"]
24. selectedImage:[UIImage imageNamed:@"tabbar_me_h"]];
25.
26. second.tabBarItem = secondItem;
27.
28.
29. [self.window setRootViewController:tabBarController];
30.
31. return YES;
32.}
33.

Alt text

  • 设置消息提醒个数即为:viewController.tabBarItem.badgeValue = @"123"
  • 有时我们需要将用户退出程序时所选中动视图控制器索引保存下来,以便下次用户重新进入程序时程序还是之前退出时的状态。
    tabBarController.delegate = self;
    并实现方法:
1.-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
2.
3. //用于保存用户偏好设置
4. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
5. [userDefaults setInteger:tabBarController.selectedIndex forKey:@"selectedIndex"];
6. [userDefaults synchronize];//强行立马写进文件(不写的话,那么就会是周期性的同步)
7.}
 
原文地址:https://www.cnblogs.com/buakaw/p/5194563.html