2009-10-23 82 views
6

我有一个涉及UINavigationController的问题。如何重置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中完成,为什么?

干杯, 安德烈亚斯

+0

发布您的一些代码,以便我们可以看到您如何推送/弹出视图控制器以及您究竟在推送和弹出什么内容。 – Jasarien 2009-10-23 12:37:51

回答

2

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

更新:

我创建了一个基于导航的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"; 
} 
+0

如何重新推送视图控制器?我已经添加了处理上述推送和弹出的代码,您有什么建议吗? – drisse 2009-10-26 07:05:49

+0

“重新推送”是什么意思?你知道了吗? – gerry3 2009-12-19 19:50:02

5

在我移动到真正的主屏幕之前,我的堆栈中有几个VC作为设置屏幕。

我雷到了这样的工作:

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

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

相关问题