如何重置一个UINavigationController?(How do I reset a UIN

2019-08-31 15:46发布

我有涉及UINavigationController的一个问题。

我有有三个视图控制器,我使用的导航控制器压入和弹出的功能之间进行切换的应用。

问题是这样的:我站在第三个视图控制器,使一个呼叫
[self.navigationController popToRootViewControllerAnimated:YES];
这使我想到的第一个视图控制器(这正是我想要的),但是当我再尝试导航到第二个视图控制器我结束了在第三出于某种原因。

有没有什么可以重新设置导航控制器,还是我这个做了错误的方式什么办法?

下面是我用推和弹出导航控制器的代码:

当用户决定启动相机下面的代码是所谓的根视图控制器。

if(self.cameraViewController == nil)
{
        CollageCameraViewController *camView = [[CollageCameraViewController alloc] init];//WithNibName:nil bundle:[NSBundle mainBundle]];
        self.cameraViewController = camView;
        [camView release];

}
[self.navigationController pushViewController:self.cameraViewController animated:NO];

下面的代码是从CollageCameraViewController(第二)称为用户已经采取了照片之后:

if(self.renderView == nil)
{
    CollageRenderViewController *renderViewController = [[CollageRenderViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
    self.renderView = renderViewController;
    [renderViewController release];
}
[self.navigationController pushViewController:self.renderView animated:YES];

当用户决定返回到主(根)视图下面的代码是从CollageRenderViewController称为:

[self.navigationController popToRootViewControllerAnimated:YES];

现在,如果我试图推动CollageCameraViewController我再次在CollageRenderViewController最终相反,这是为什么?

干杯,安德烈亚斯

Answer 1:

我有几个风投在我的堆栈设定屏幕之前,我移动到真正的主屏幕。

我得到了我这样的工作:

HomeViewController *hvc = [[HomeViewController alloc] init];
[self.navigationController pushViewController:hvc animated:YES];
self.navigationController.viewControllers = [[NSArray alloc] initWithObjects:hvc, nil];

我推HomeViewController于是重写栈只有主屏幕。



Answer 2:

一旦你弹出到根视图控制器,它是“复位”。 您与navigationController下一个动作要推(或重新推)相应的视图控制器。 你不应该试图“导航”,通过堆栈。

更新:

我创建了一个基于导航的iPhone项目,以测试你的代码和它的作品。 我的每一个的三个视图具有单个按钮抽头时,它发送其控制器其IBAction为消息。

这里是我的代码:

RootViewController.h:

@class SecondViewController;

@interface RootViewController : UIViewController {
    SecondViewController *secondViewController;
}

@property (nonatomic, retain) SecondViewController *secondViewController;

- (IBAction)pushSecondVC;

@end

RootViewController.m:

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController

@synthesize secondViewController;

- (IBAction)pushSecondVC {
    if(self.secondViewController == nil)
    {
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        self.secondViewController = secondVC;
        [secondVC release];
    }
    [self.navigationController pushViewController:self.secondViewController animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Root View";
}

SecondViewController.h:

#import <UIKit/UIKit.h>

@class ThirdViewController;

@interface SecondViewController : UIViewController {
    ThirdViewController *thirdViewController;
}

@property (nonatomic, retain) ThirdViewController *thirdViewController;

- (IBAction)pushThirdVC;

@end

SecondViewController.m:

#import "SecondViewController.h"
#import "ThirdViewController.h"

@implementation SecondViewController

@synthesize thirdViewController;

- (IBAction)pushThirdVC {
    if(self.thirdViewController == nil)
    {
        ThirdViewController *thirdVC = [[ThirdViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
        self.thirdViewController = thirdVC;
        [thirdVC release];
    }
    [self.navigationController pushViewController:self.thirdViewController animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"2nd View";
}

ThirdViewController.h:

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController {
}

- (IBAction)popToRoot;

@end

ThirdViewController.m:

#import "ThirdViewController.h"

@implementation ThirdViewController

- (IBAction)popToRoot {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"3rd View";
}


文章来源: How do I reset a UINavigationController?