推送 ViewController 时清除 navigationController 堆栈
Posted
技术标签:
【中文标题】推送 ViewController 时清除 navigationController 堆栈【英文标题】:Clear navigationController stack when push ViewController 【发布时间】:2014-03-19 10:53:12 【问题描述】:我有一个导航控制器,可以推送一些视图控制器。它工作正常,但我无法清除导航控制器堆栈,所以我有内存警告......
这是我想做的:
Viewcontroller 1 : HomeViewController
ViewController 2 : LandingPageViewController
ViewController 3 : DetailsInnovViewController
homeViewController -> Push -> landingPageViewController -> Push -> detailsPageViewController -> Push -> homeViewController
它可以工作,但我想要一个新的 homeViewController,而不是旧的。 Si 可以从导航控制器中删除。
这是我如何使用我的 navigationController 从 detailsPageViewController 推送 homeViewController :
- (void)pushHomeVC
[LoginModel incrementCountedInnov];
for (UIViewController* vc in self.viewControllers)
if ([vc isKindOfClass:[DetailsInnovViewController class]])
DetailsInnovViewController* dpvc = (DetailsInnovViewController*)vc;
[dpvc bannerHide];
break;
for (UIViewController* vc in self.viewControllers)
if ([vc isKindOfClass:[HomeViewController class]])
[self popToRootViewControllerAnimated:NO];
[self pushViewController:vc animated:YES];
break;
感谢您的帮助!
【问题讨论】:
不幸的是 HomeViewController 没有设置为 root... 【参考方案1】:我知道这是一个老问题,但我最近遇到了类似的问题,发现这个解决方案对我有用:
[navigationController pushViewController:viewController animated:YES];
[navigationController setViewControllers:@[navigationController.topViewController]];
【讨论】:
【参考方案2】:我已经找到了如何做到这一点:
- (void)pushHomeVC
[LoginModel incrementCountedInnov];
for (UIViewController* vc in self.viewControllers)
if ([vc isKindOfClass:[DetailsInnovViewController class]])
DetailsInnovViewController* dpvc = (DetailsInnovViewController*)vc;
[dpvc bannerHide];
break;
HomeViewController* homeVC = (HomeViewController*)[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"home-vc"];
[self pushViewController:homeVC animated:YES];
还有我的 pushViewController 方法:
- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
@synchronized(self)
if (isTransitioning == YES)
[futureViewControllers addObject:viewController];
else
isTransitioning = YES;
if (self.viewControllers.count > 6)
[super popToRootViewControllerAnimated:NO];
[super pushViewController:viewController animated:(BOOL)animated];
谢谢!
【讨论】:
以上是关于推送 ViewController 时清除 navigationController 堆栈的主要内容,如果未能解决你的问题,请参考以下文章
推送通知 - 使用 SceneDelegate 在通知点击时推送 ViewController
当应用程序完全关闭时,推送通知点击上的推送 ViewController 将不起作用
推送其他viewcontroller时隐藏uisearchdisplaycontroller的navigationbar