我遇到涉及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 :(得分:5)
在移动到真正的主屏幕之前,我的堆栈中有几个VC作为设置屏幕。
我得到了这样的工作:
HomeViewController *hvc = [[HomeViewController alloc] init];
[self.navigationController pushViewController:hvc animated:YES];
self.navigationController.viewControllers = [[NSArray alloc] initWithObjects:hvc, nil];
我按下HomeViewController,然后重写堆栈只有主屏幕。
答案 1 :(得分: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";
}