推送ViewController时清除navigationController堆栈

时间:2014-03-19 10:53:13

标签: ios objective-c uinavigationcontroller stack

我有一个推送一些viewcontroller的导航控制器。它工作正常,但我无法清除导航控制器堆栈,因此我得到了内存警告...

这是我想要做的:

Viewcontroller 1 : HomeViewController
ViewController 2 : LandingPageViewController
ViewController 3 : DetailsInnovViewController

homeViewController -> Push -> landingPageViewController -> Push -> detailsPageViewController -> Push -> homeViewController

它可以工作,但我想要一个新的homeViewController,而不是旧的。 Si可以从导航控制器中删除。

以下是我如何使用navigationController从detailPageViewController推送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;
        }
    }
}

帮助你!

2 个答案:

答案 0 :(得分:2)

我知道这是一个老问题,但我最近遇到了类似的问题,发现这个解决方案对我有用:

[navigationController pushViewController:viewController animated:YES];
[navigationController setViewControllers:@[navigationController.topViewController]];

答案 1 :(得分:0)

我已经找到了如何做到这一点:

- (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];
        }
    }
}

Thx!