分栏控制器

分栏控制器

分栏控制器基础

分栏控制器有点像导航控制器,但分栏控制器管理视图控制器时和导航控制器不同的地方在于,导航控制器中的视图控制器具有层级关系,而分栏控制器则是平行关系
分栏控制器对于视图控制器的添加应该以数组形式添加
其他地方类似于导航控制器,比如导航栏和导航栏按钮
只是分栏控制器的每个视图控制器对应一个按钮
先给出代码:

//
//  SceneDelegate.m
//  分栏控制器基础
//
//  Created by 朱敬业 on 2023/6/4.
//

#import "SceneDelegate.h"
#import "VC01.h"
#import "VC02.h"
#import "VC03.h"
@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    VC01* vc01 = [[VC01 alloc] init] ;
    VC02* vc02 = [[VC02 alloc] init] ;
    vc02.view.backgroundColor = UIColor.greenColor ;
    VC03* vc03 = [[VC03 alloc] init] ;
    vc03.view.backgroundColor = UIColor.blueColor ;
    vc01.title = @"视图1" ;
    vc02.title = @"视图2" ;
    vc03.title = @"视图3" ;
    vc01.view.backgroundColor = UIColor.orangeColor ;//视图控制器的点语法调用会调用viewdidload函数
    UITabBarController* tbcontroller = [[UITabBarController alloc] init] ;
    NSArray* array = [NSArray arrayWithObjects:vc01, vc02, vc03, nil] ;
    tbcontroller.viewControllers = array ;//分栏控制器的视图管理需要复制数组
    tbcontroller.tabBar.backgroundColor = UIColor.whiteColor ;
    self.window.rootViewController = tbcontroller ;
    tbcontroller.selectedIndex = 2 ;//分栏控制器当前的视图的索引
    if (tbcontroller.selectedViewController == vc03) {
        NSLog(@"yes") ;
    }
    tbcontroller.tabBar.translucent = NO ;
    //translucent属性表示是否是透明
    
}


- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end

//
//  VC01.m
//  分栏控制器基础
//
//  Created by 朱敬业 on 2023/6/4.
//

#import "VC01.h"

@interface VC01 ()

@end

@implementation VC01

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UITabBarItem* btn01 = [[UITabBarItem alloc] initWithTitle:@"111" image:nil tag:101] ;
//    self.tabBarItem = btn01 ;
    UITabBarItem* btn02 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:102] ;
    //创建分栏控制器按钮时可以出实话他的按钮风格
    btn02.badgeValue = @"22" ;//按钮上的提示信息,常用于表示未读信息
    self.tabBarItem = btn02 ;//每个视图对应一个分栏控制器按钮
}

/*
#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

一些新的属性和对象可以看看我代码上的注释,应该挺好理解的

分栏控制器高级

其实就是分栏控制器的代理协议方法的使用

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
    NSLog(@"即将开始编辑") ;
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
    NSLog(@"即将结束编辑") ;
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
    NSLog(@"已经结束编辑") ;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"选中控制器对象") ;  
}

方法的作用和上面的输出语句相对应
主要解释一下编辑是什么意思
在这里插入图片描述
当视图控制器数量大于5个是,第五个和之后的视图控制器会交由一个导航控制器管理,但你认可以通过edit控制那几个视图控制器交给分栏控制器,哪几个交给导航控制器
且注意,edit更改是也会改变array中视图控制器对象的顺序

//
//  SceneDelegate.m
//  分栏控制器高级
//
//  Created by 朱敬业 on 2023/6/4.
//

#import "SceneDelegate.h"
#import "VC01.h"
#import "VC02.h"
#import "VC03.h"
#import "VC04.h"
#import "VC05.h"
#import "VC06.h"
@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    [self.window makeKeyAndVisible] ;
    VC01* vc01 = [[VC01 alloc] init] ;
    VC02* vc02 = [[VC02 alloc] init] ;
    VC03* vc03 = [[VC03 alloc] init] ;
    VC04* vc04 = [[VC04 alloc] init] ;
    VC05* vc05 = [[VC05 alloc] init] ;
    VC06* vc06 = [[VC06 alloc] init] ;
    vc01.view.backgroundColor = UIColor.greenColor ;
    vc02.view.backgroundColor = UIColor.grayColor ;
    vc03.view.backgroundColor = UIColor.blueColor ;
    vc04.view.backgroundColor = UIColor.redColor ;
    vc05.view.backgroundColor = UIColor.yellowColor ;
    vc06.view.backgroundColor = UIColor.purpleColor ;
    vc01.title = @"视图1" ;
    vc02.title = @"视图2" ;
    vc03.title = @"视图3" ;
    vc04.title = @"视图4" ;
    vc05.title = @"视图5" ;
    vc06.title = @"视图6" ;
    NSArray* array = [NSArray arrayWithObjects:vc01, vc02, vc03, vc04, vc05, vc06, nil] ;
    UITabBarController* tbcontroller = [[UITabBarController alloc] init] ;
    tbcontroller.viewControllers = array ;
    tbcontroller.tabBar.backgroundColor = UIColor.whiteColor ;
    tbcontroller.tabBar.tintColor = UIColor.redColor ;
    self.window.rootViewController = tbcontroller ;
    tbcontroller.delegate = self ;//将self设置为tbcontroller对象
   }

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
    NSLog(@"即将开始编辑") ;
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
    NSLog(@"即将结束编辑") ;
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
    NSLog(@"已经结束编辑") ;
    NSLog(@"%@",viewControllers[1]) ;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"选中控制器对象") ;  
}

- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值