简体   繁体   English

iPhone-导航控制器不起作用

[英]iPhone - Navigation controller not working

I'm going mad using navigation controllers on the iPhone. 我在iPhone上使用导航控制器会发疯。 I have an app, with a main xib (the one with the window) inside wich I have put a NavigationController, inside wich I have a viewController. 我有一个应用程序,在其中有一个主xib(带有窗口的一个),我已经放置了NavigationController,在其中有一个viewController。 Everything is connected and the ViewController is defined with the correct inherited class name. 一切都已连接,并使用正确的继承类名称定义了ViewController。

In the didFinishLaunchingWithOptions, i have : 在didFinishLaunchingWithOptions中,我有:

[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

In the .h I have : 在.h中,我有:

@interface MainAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet UINavigationController* navigationController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController* navigationController;

@end

Then in the First ViewController I have a button connected to this method : 然后在第一个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];
}

all items in the main xib seems to be connected... and retained by the properties. 主xib中的所有项目似乎都已连接...并由属性保留。 The AppDelegate with its window and navigationController... the Window rootviewcontroller with the same navigationController... and the file owner with the app delegate... 带有窗口和navigationController的AppDelegate ...带有相同navigationController的Window rootviewcontroller ...和带有应用程序委托的文件所有者...

Everything runs fine, but the preferences window never appears... 一切运行正常,但“偏好设置”窗口从未出现...

Can you see why ? 你知道为什么吗?

If needed, I must say that this first view controller makes the camera interface appear and put an overlay over it. 如果需要的话,我必须说这个第一个视图控制器使摄像头界面出现并在其上放置覆盖。 The button is onto this overlay. 该按钮位于此叠加层上。 The imagePicker is show like this in viewDidAppear : imagePicker在viewDidAppear中显示如下:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
    [picker release];

EDIT : In viewDidAppear, self.navigationController is ok at the start and end of method. 编辑:在viewDidAppear中,self.navigationController在方法的开始和结束处都可以。 In definePreferences, self.navigationController is nil. 在definePreferences中,self.navigationController为nil。 Nothing is called beetween those two calls. 在这两个调用之间没有任何东西被称为。 Nothing 没有

EDIT : The problem may come from the way I init the viewController on which the button is. 编辑:问题可能来自我初始化按钮所在的viewController的方式。 Here is the method called from the firstView called by the Navigation Controller. 这是从导航控制器调用的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];
}

But... how should I do ? 但是...我该怎么办?

First, never call an IBAction setPreferences: . 首先,切勿调用IBAction setPreferences: That violates KVC, and can eventually cause all kinds of bizarre behaviors. 这违反了KVC,并最终可能导致各种奇怪的行为。 setX: is a reserved name for the setter of the property named x . setX:是名为x的属性的设置方法的保留名称。

You should not be creating a nav controller in this method (ie navController ). 您不应该在此方法中创建导航控制器(即navController )。 You should be using the one you created in the NIB ( self.navigationController ). 您应该使用在NIB中创建的那个( self.navigationController )。 Check if that is nil. 检查是否为零。 If it is, then you either didn't set up a navigation controller in the NIB, or you didn't wire it to this view controller. 如果是这样,那么您要么没有在NIB中设置导航控制器,要么没有将其连接到该视图控制器。

You should also verify that nextWindow is non-nil. 您还应该验证nextWindow为非nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM