我正在尝试实现一个基本的UINavigationController
,并且我遇到导航控制器显示错误视图的问题。
我首先在Window Based Application
中创建了一个Xcode 4
,它为我提供了以下文件:spellingAppDelegate.h
,spellingAppDelegate.m
和MainWindows.xib
。然后我添加了一个新的UIViewController
子类并将其命名为gameViewController
。
以下是我的myAppDelegate.h
#import <UIKit/UIKit.h>
@interface spellingAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@end
以下是我的myAppDelegate.m
#import "spellingAppDelegate.h"
#import "gameViewController.h"
#import "resultViewController.h"
@implementation spellingAppDelegate
@synthesize window = window;
@synthesize navigationController = navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window makeKeyAndVisible];
// create the MyView controller instance:
gameViewController *controller = [[gameViewController alloc] initWithNibName:@"gameViewController" bundle:nil];
// set the title that appears in the navigation bar:
[controller.navigationItem setTitle:@"Main View"];
// create the Navigation Controller instance:
UINavigationController *newnav = [[UINavigationController alloc] initWithRootViewController:controller];
// set the navController property:
[self setNavigationController:newnav];
// release both controllers:
[newnav release];
[controller release];
// add the Navigation Controller's view to the window:
[window addSubview:[navigationController view]];
return YES;
}
我的印象是,如果我运行上面的代码,应用程序将从gameViewController.xib开始。但是,它显示MainWindow.xib。我知道我可能会错过一些基本的东西,但我无法弄清楚我做错了什么。谢谢。
答案 0 :(得分:3)
试试这个会起作用
navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
答案 1 :(得分:1)
如果以这种方式执行,它将起作用
demoViewController *controller = [[demoViewController alloc] initWithNibName:@"demoViewController" bundle:nil];
// set the title that appears in the navigation bar:
[controller.navigationItem setTitle:@"Main View"];
// create the Navigation Controller instance:
UINavigationController *newnav = [[UINavigationController alloc] initWithRootViewController:controller];
// set the navController property:
// [self setNavigationController:newnav];
[self.window addSubview:newnav.view];
// release both controllers:
[newnav release];
[controller release];
答案 2 :(得分:1)
应用程序的窗口在nib文件中定义。通常,此笔尖为MainWindow.xib
,但您可以通过编辑应用程序的info.plist
文件来更改它。
您可以在此nib中启动要在启动时加载的视图控制器的引用(请参阅基于视图的应用程序模板),或者可以使用应用程序委托中的application:didFinishLaunchingWithOptions:
方法获得相同的效果
在iOS 4.0及更高版本中,我将使用UIWindow
属性rootViewController
添加视图控制器。例如:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
AViewController * aViewController = [[AViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = aViewController;
[aViewController release];
[self.window makeKeyAndVisible];
return YES;
}