IOS UITabBarController和UINavigationController实现多级控制器管理

本文介绍了在iOS开发中如何结合使用UITabBarController和UINavigationController来管理多级控制器。通过创建UITabBarController,为每个Tab添加UINavigationController作为子控制器,可以实现平级页面间的切换。同时,文章讨论了在实际操作中遇到的崩溃问题,提供了避免崩溃的处理方法,确保代码的稳定运行。
摘要由CSDN通过智能技术生成

UITabBarController的使用

引入:上一篇博客有提到UINavigationController的使用,这篇博客我先重点介绍这个和它使用一样广泛的UITabBarController,我们比较常见的是微信的下方四个按钮,可以切换不同的页面。每个页面都对应着不同的控制器,在UITabBarController中,管理着若干子控制器,他们之间是并列的关系,在添加到TabBar之后就不会被销毁,只会相互切换,实测过tabBar1和tabBar2的视图会相继调用willMoveToWindow和didMoveToWindow,因此本质是不断地切换在window上的显示权。
下面结合源码直接看官方给出的属性和方法API很简短,我会在重要的地方给出注释。
UITabBarController:

#import <Foundation/Foundation.h>
#import <UIKit/UIViewController.h>
#import <UIKit/UIViewControllerTransitioning.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UITabBar.h>

NS_ASSUME_NONNULL_BEGIN
//申明这里会用到这四个类
@class UIView, UIImage, UINavigationController, UITabBarItem;
//申明了一个协议
@protocol UITabBarControllerDelegate;
//UITabBarController继承于UIViewController,实现UITabBarDelegate和NSCoding协议(NSCoding表面支持归档)
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
//若干控制器成员
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//对vc栈整体更新
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//当前被选中的控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; // This may return the "More" navigation controller if it exists.
//被选中的控制器的下标
@property(nonatomic) NSUInteger selectedIndex;
//返回更多导航控制器
@property(nonatomic, readonly) UINavigationController *moreNavigationController __TVOS_PROHIBITED; // Returns the "More" navigation controller, creating it if it does not already exist.
//控制器数组
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers __TVOS_PROHIBITED; // If non-nil, then the "More" view will include an "Edit" button that displays customization UI for the specified controllers. By default, all view controllers are customizable.
//tabBar实例
@property(nonatomic,readonly) UITabBar 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值