没用storyboard
没有用到storyboard的话,需要在系统初始化完成之前进行恢复
需要在这个方法里面
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions {
CGRect frame = [UIScreen mainScreen].bounds;
self.window = [[UIWindow alloc] initWithFrame:frame];
self.window.backgroundColor = [UIColor whiteColor];
//设置恢复的唯一标识 如果没有对window进行保存的相关操作,则可以不设置
self.window.restorationIdentifier = NSStringFromClass([UIWindow class]);
PGTabBarViewController *tabBar = [PGTabBarViewController sharedGTTabBar];
//设置恢复的唯一标识
tabBar.restorationIdentifier = NSStringFromClass([PGTabBarViewController class]);
self.window.rootViewController = tabBar;
return true;
}
PGTabBarViewController关键代码
- (void)viewDidLoad {
[super viewDidLoad];
FirstController *firstController = [[FirstController alloc] init];
[self setupChildViewController:firstController title:@"第一"];
SecondTableController *second = [[SecondTableController alloc] initWithNibName:@"SecondTableController" bundle:nil];
[self setupChildViewController:second title:@"第二"];
ThirdViewController *third = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self setupChildViewController:third title:@"第三"];
FourTableController *four = [[FourTableController alloc] init];
[self setupChildViewController:four title:@"第四"];
}
- (void)setupChildViewController:(UIViewController *)controller title:(NSString *)title {
UITabBarItem *tabBarItem = controller.tabBarItem;
tabBarItem.title = title;
[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateNormal];
[tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateSelected];
tabBarItem.image = [self imageWithColor:[UIColor greenColor]];
PGNavigationController *navigationController = [[PGNavigationController alloc] initWithRootViewController:controller];
//设置需要恢复的唯一标识
navigationController.restorationIdentifier = NSStringFromClass([controller class]);
[self addChildViewController:navigationController];
}
FirstController跟FourTableController没有用到xib,恢复标识在viewDidLoad设置即可。
- (void)viewDidLoad {
[super viewDidLoad];
self.restorationIdentifier = NSStringFromClass([self class]);
}
SecondTableController跟ThirdViewController用到了xib,则需要在initWithNibName方法进行设置
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.restorationIdentifier = NSStringFromClass([self class]);
}
return self;
}
子页面指的是没有在willFinishLaunchingWithOptions跟PGTabBarViewController里面初始化的页面,也就是不是PGTabBarViewController直接嵌套的页面
如果还有子页面,则需要设置
@interface PersonDetailController ()<UIViewControllerRestoration>
@end
@implementation PersonDetailController
- (void)viewDidLoad {
[super viewDidLoad];
self.restorationIdentifier = NSStringFromClass(self.class);
self.restorationClass = self.class;
}
#pragma mark - UIViewControllerRestoration
+ (nullable UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray<NSString *> *)identifierComponents coder:(NSCoder *)coder {
PersonDetailController *ctrl = [[PersonDetailController alloc]init];
return ctrl;
}
@end
保存与恢复数据
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
}
网友评论