UITabBarController的常用方法,属性

示例代码

#import "AppDelegate.h"
#import "VCFirst.h"
#import "VCSecond.h"
#import "VCThird.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //显示window
    [self.window makeKeyAndVisible];

    //创建控制器一
    VCFirst* vcFirst = [[VCFirst alloc] init];


    //2
    VCSecond* vcSecond = [[VCSecond alloc] init];

    vcSecond.view.backgroundColor = [UIColor yellowColor];

    //3
    VCThird* vcThird = [[VCThird alloc] init];

    vcThird.view.backgroundColor = [UIColor orangeColor];

    vcFirst.title = @"视图一";

    vcSecond.title = @"视图二";

    vcThird.title = @"视图三";



    //在调用点语法时会调用其didload函数
    vcFirst.view.backgroundColor = [UIColor blueColor];

    //创建分栏控制器对象
    UITabBarController* tbController = [[UITabBarController alloc] init];

    //创建一个控制器数组对象
    //将所有要被分栏控制器管理的对象添加到数组中
    //分栏顺序取决于数组中的顺序
    NSArray* arrayVC = [NSArray arrayWithObjects:vcFirst, vcSecond, vcThird, nil];

    //将分栏视图控制器管理数组赋值
    tbController.viewControllers = arrayVC;

    //将分栏控制器作为根视图的控制器
    self.window.rootViewController = tbController;

    //设置选中的视图控制器去的索引
    //通过索引来确定显示哪一个控制器
    tbController.selectedIndex = 2;

    //当前选中的控制器对象
    if (tbController.selectedViewController == vcThird) {
        NSLog(@"当前显示的是控制器三");
    }

    //设置分栏控制器的工具栏的透明度
    tbController.tabBar.translucent = NO;

    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 invalidate graphics rendering callbacks. 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 active 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
#import "VCFirst.h"

@interface VCFirst ()

@end

@implementation VCFirst

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    //创建一个分栏按钮对象
    //参数1:文字
    //参数2:图片
    //参数3:设置按钮值
    //方法一
    UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"1"] tag:101];

    self.tabBarItem = tabBarItem;

//    UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:101];
//    //按钮右上角的提示信息
//    //通常用来提示未读信息
//    tabBarItem.badgeValue = @"22";
//
//    self.tabBarItem = tabBarItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

心得体会

  1. 点语法调用时会调用该页的didload函数,注意不要使内容重复设置,使得该有的没了
  2. 要将分栏控制器作为根视图

猜你喜欢

转载自blog.csdn.net/KevinAshen/article/details/81179994