沒用storyboard
沒有用到storyboard的話,需要在系統(tǒng)初始化完成之前進行恢復
需要在這個方法里面
- (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];
//設(shè)置恢復的唯一標識 如果沒有對window進行保存的相關(guān)操作,則可以不設(shè)置
self.window.restorationIdentifier = NSStringFromClass([UIWindow class]);
PGTabBarViewController *tabBar = [PGTabBarViewController sharedGTTabBar];
//設(shè)置恢復的唯一標識
tabBar.restorationIdentifier = NSStringFromClass([PGTabBarViewController class]);
self.window.rootViewController = tabBar;
return true;
}
PGTabBarViewController關(guān)鍵代碼
- (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];
//設(shè)置需要恢復的唯一標識
navigationController.restorationIdentifier = NSStringFromClass([controller class]);
[self addChildViewController:navigationController];
}
FirstController跟FourTableController沒有用到xib,恢復標識在viewDidLoad設(shè)置即可。
- (void)viewDidLoad {
[super viewDidLoad];
self.restorationIdentifier = NSStringFromClass([self class]);
}
SecondTableController跟ThirdViewController用到了xib,則需要在initWithNibName方法進行設(shè)置
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.restorationIdentifier = NSStringFromClass([self class]);
}
return self;
}
子頁面指的是沒有在willFinishLaunchingWithOptions跟PGTabBarViewController里面初始化的頁面,也就是不是PGTabBarViewController直接嵌套的頁面
如果還有子頁面,則需要設(shè)置
@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
保存與恢復數(shù)據(jù)
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
}