需求:
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⃣️欢迎有更好的思路一起交流