iOS开发之简述底部tabbar上某一vc加载不同界面的切换方法

本文探讨了在iOS开发中如何处理底部tabbar根据用户操作动态切换默认加载界面的问题。错误做法是在启动时请求接口决定加载界面,可能导致黑屏和网络问题。正确的解决方案是采用按需加载,通过父VC管理子VC的显示和隐藏,以实现平滑的界面切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:
tabbar默认加载A界面,点击了某一按钮/请求了某一个接口后默认加载B界面

1⃣️
错误思路?:
在启动时请求接口,根据请求的接口判断当前需要加载的vc
错误思路原因?:
网络请求再快的情况下都需要时间,还要考虑网络连接超时界面会持续黑屏,请求失败时还需对应处理失败展示,总之就是启动时会黑屏。

2⃣️
正确做法?:
按需加载
正确思路?:
新建一个父vc,在父vc里处理加载的子vc,子view,请求改变状态时隐藏当前正在展示的view,下面贴代码示例

launch.m(appdelegate.m)
// 注意这里的步骤,先设置tabbar,再发送通知,不然发送时监听通知对象还是nill,是接受不到通知的,对这里还有疑问的小伙伴可移步去搜一下通知的使用。
  JTTabBarController *tabbarVc = [[JTTabBarController alloc] init];
        keyWindow.rootViewController = tabbarVc;
        keyWindow.backgroundColor = [UIColor whiteColor];
        [keyWindow makeKeyAndVisible];
        
        // 请求订单状态,改变当前加载界面
        [self launcherRequestWithScanOrder:^(BOOL res,NSInteger status) {
            
            // 发送通知
            [[NSNotificationCenter defaultCenter] postNotificationName:@"JTScanShowStatus" object:nil userInfo:@{
                                                                                                                 @"show":[NSString stringWithFormat:@"%d",res],
                                                                                                                 @"status":[NSString stringWithFormat:@"%ld",status]
                                                                                                                 }];
        }];

JTHomeBaseViewController.m

@interface JTHomeBaseViewController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) HomeViewController *homeVc;
@property (nonatomic, strong) TimingViewController *timingVc;

@end



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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(configureWithViewLoad:) name:@"JTScanShowStatus" object:nil];
    
}

- (void)configureWithViewLoad:(NSNotification *)notice {
    
    NSDictionary *info = notice.userInfo;
    if ([info objectForKey:@"show"]) {
       NSString *show = [info objectForKey:@"show"];
       
        if (show.integerValue == 0) {
            
            if (self.timingVc.view.hidden == NO) {
                self.timingVc.view.hidden = YES;
            }
            
            self.homeVc = [[HomeViewController alloc]init];
            [self cxt_addChildViewController:self.homeVc viewConfigurator:^(UIView *containerView, UIViewController *targetVC) {
                [containerView addSubview:targetVC.view];
                [targetVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.top.bottom.mas_equalTo(self.view);
                }];
            }];
            
        } else {
            
            if (self.homeVc.view.hidden == NO) {
                self.homeVc.view.hidden = YES;
            }
            
            self.timingVc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil]instantiateViewControllerWithIdentifier:@"timingVc"];
            [self cxt_addChildViewController:self.timingVc viewConfigurator:^(UIView *containerView, UIViewController *targetVC) {
                [containerView addSubview:targetVC.view];
                [targetVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.top.bottom.mas_equalTo(self.view);
                }];
            }];
        }
    }
}

- (void)cxt_addChildViewController:(UIViewController *)controller viewConfigurator:(void(^)(UIView *containerView, UIViewController *targetVC))configurator {
    [controller beginAppearanceTransition:YES animated:NO];
    [controller willMoveToParentViewController:self];
    [self addChildViewController:controller];
    if (configurator) {
        configurator(self.view, controller);
    }
    [controller didMoveToParentViewController:controller];
    [controller endAppearanceTransition];
}

3⃣️欢迎有更好的思路一起交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值