最近遇到一个需求,需要在深层页面的时候,把页面状态挂起,返回首页做其他操作,然后某一时刻再进入挂起的页面继续操作。为解决这个问题,我这边用了单利来指向当前的VC,防止pop回首页的时候页面被系统释放,然后需要返回挂起页面时再做push处理。
主要代码如下:
//1.在baseVC中显示挂起入口代码
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[GDWindowManager sharedManager].moveButton.hidden = self.navigationController.viewControllers.count < 2 && ![GDWindowManager sharedManager].moveButton.selected;
}
//2.在按钮方法中取到当前挂起VC保存到单利中,再次按下返回挂起的VC时做push处理
- (void)buttonAction:(UIButton *)button {
button.selected = !button.selected;
UIViewController *currentVC = [self.keywindow rootViewController];
while (true) {
if (currentVC.presentedViewController) {
currentVC = currentVC.presentedViewController;
} else if ([currentVC isKindOfClass:[UINavigationController class]] && [(UINavigationController *)currentVC topViewController]) {
currentVC = [(UINavigationController *)currentVC topViewController];
} else if ([currentVC isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)currentVC;
currentVC = tab.selectedViewController;
} else {
break;
}
}
if (button.selected) {
self.target = currentVC;
UIViewController *firstVC = currentVC.navigationController.viewControllers.firstObject;
currentVC.navigationController.viewControllers = @[firstVC];
} else {
[currentVC.navigationController pushViewController:self.target animated:YES];
self.target = nil;
}
}
以上方式解决了挂起当前页面的问题,本篇GitHubDemo,希望能帮到你...