iPhone - 导航控制器不工作
我在 iPhone 上使用导航控制器快要疯了。 我有一个应用程序,里面有一个主xib(带有窗口的那个),里面我放了一个NavigationController,里面有一个viewController。一切都已连接,并且 ViewController 是使用正确的继承类名定义的。
在 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 及其窗口和导航控制器...具有相同导航控制器的 Window rootviewcontroller...以及具有应用程序委托的文件所有者...
一切运行正常,但首选项窗口从未出现...
您能明白为什么吗?
如果需要,我必须说这个第一个视图控制器使相机界面出现并在其上放置覆盖层。该按钮位于此覆盖层上。 imagePicker 在 viewDidAppear 中显示如下:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
[picker release];
编辑: 在 viewDidAppear 中, self.navigationController 在方法的开始和结束处都可以。 在definePreferences中,self.navigationController为nil。这两个调用之间没有调用任何内容。 没什么
编辑: 问题可能来自于我初始化按钮所在的 viewController 的方式。 这是从导航控制器调用的第一个视图调用的方法。
- (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];
}
但是……我该怎么办?
I'm going mad using navigation controllers on the 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. Everything is connected and the ViewController is defined with the correct inherited class name.
In the didFinishLaunchingWithOptions, i have :
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
In the .h I have :
@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 :
- (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. The AppDelegate with its window and navigationController... the Window rootviewcontroller with the same navigationController... and the file owner with the app delegate...
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 :
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.
In definePreferences, self.navigationController is 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.
Here is the method called from the firstView called by the Navigation Controller.
- (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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,永远不要调用 IBAction
setPreferences:
。这违反了 KVC,并最终会导致各种奇怪的行为。setX:
是名为x
的属性的 setter 的保留名称。您不应该在此方法中创建导航控制器(即
navController
)。您应该使用在 NIB 中创建的控制器 (self.navigationController
)。检查是否为零。如果是,那么您要么没有在 NIB 中设置导航控制器,要么没有将其连接到该视图控制器。您还应该验证
nextWindow
是否为非零。First, never call an IBAction
setPreferences:
. That violates KVC, and can eventually cause all kinds of bizarre behaviors.setX:
is a reserved name for the setter of the property namedx
.You should not be creating a nav controller in this method (i.e.
navController
). You should be using the one you created in the 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.You should also verify that
nextWindow
is non-nil.我终于解决了这个问题。
请参阅https://stackoverflow.com/questions/5317968 /iphone-camera-overlay-going-down-after-a-modal-view-transition
我头发少了一些......:-)
I've finaly solved the problem.
See https://stackoverflow.com/questions/5317968/iphone-camera-overlay-going-down-after-a-modal-view-transition
I have some hair less... :-)