iPhone - 导航控制器不工作
我在iPhone上使用导航控制器发疯。 我有一个应用程序,与主xib(与窗口之一)里面至极放置一个NavigationController,里面至极我有一个viewController。所有东西都连接在一起,ViewController使用正确的继承类名来定义。iPhone - 导航控制器不工作
在didFinishLaunchingWithOptions,我有:
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
在.H我有:
@interface MainAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet UINavigationController* navigationController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController* navigationController;
@end
然后在首先的ViewController我具有连接到该方法中的按钮:
- (IBAction) definePreferences:(id)sender {
PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:@"Preferences" bundle:nil] autorelease];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
}
主xib中的所有项目似乎都被连接...并被属性保留。在AppDelegate中有它的窗口和navigationController ...窗口RootViewController的具有相同navigationController ......与应用程序的委托文件的所有者...
一切都正常运行,但从来没有出现在首选项窗口...
你能看到为什么吗?
如果需要,我必须说,这第一个视图控制器使相机接口出现并覆盖它。该按钮位于此覆盖层上。所述imagePicker是示出像这样在viewDidAppear:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
[picker release];
编辑: 在viewDidAppear,self.navigationController是在开始和方法的端部确定。 在definePreferences中,self.navigationController为零。这两个电话之间什么都不叫。 Nothing
编辑: 问题可能来自我初始化按钮所在的viewController的方式。 以下是由导航控制器调用的firstView调用的方法。
- (void) viewDidAppear:(BOOL)animated {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
// Insert the overlay
OverlayViewController* overlayController = [[OverlayViewController alloc] initWithNibName:@"Overlay" bundle:nil];
picker.cameraOverlayView = overlayController.view;
// Show the picker:
[self presentModalViewController:picker animated:NO];
[picker release];
[super viewDidAppear:YES];
}
但是......我该怎么办?
首先,永远不要调用IBAction setPreferences:
。这违反了KVC,并最终会导致各种奇怪的行为。 setX:
是名为x
的属性的设置者的保留名称。
您不应该使用此方法创建导航控制器(即navController
)。您应该使用您在NIB中创建的那个(self.navigationController
)。检查是否为零。如果是,那么你要么没有在NIB中设置导航控制器,要么你没有将它连接到这个视图控制器。您应该确认nextWindow
不是零。
@Rob Napier:好的,我重命名了这个方法。我只调用[self.navigationController presentModalViewController:nextWindow animated:YES];在nextWindow分配之后。 nextWindow不是零。仍然没有... self.navigationController是零,但我可以检查xib中的所有内容,所有项目似乎都已连接...并由属性保留。带有窗口和导航控制器的AppDelegate ...具有相同导航控制器的窗口rootviewcontroller ...以及具有应用程序委托的文件所有者...对发生什么问题有任何想法? – Oliver 2011-03-15 01:15:14
“self.navigationController为零”←这是你的问题。在一个nil对象上调用'presentModalViewController:animated:'不会做任何事情。 – Anomie 2011-03-15 01:42:37
@Anomie:是的,我知道,但是怎么了?事情似乎很好连接。通话似乎是好的。所以呢... ? – Oliver 2011-03-15 01:57:47
你检查你的'setPreferences:'方法实际上是被当你触摸按钮时调用,例如用日志语句或调试器断点? – Anomie 2011-03-15 01:31:51
@Anomie:当然 – Oliver 2011-03-15 01:35:10
请注意,您的窗口和navigationController都不会被保留,因为您已经在ivars上设置了IBOutlet而不是属性。因此,伊娃最终直接被设置,而不用通过可以“保留”它们的setter方法。 – Anomie 2011-03-15 02:22:31