iOS app狀態(tài)保存與恢復

沒用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];
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容