UINavigationController 显示错误的视图
我正在尝试实现一个基本的 UINavigationController ,但遇到了导航控制器显示错误视图的问题。
我首先在 Xcode 4 中创建一个基于窗口的应用程序,它为我提供了以下文件:spellingAppDelegate.h
、spellingAppDelegate.m< /code> 和
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。我知道我可能错过了一些基本的东西,但我不知道我做错了什么。谢谢。
I'm trying to implement a basic UINavigationController
and I'm running into an issue with the navigation controller displaying a wrong view.
I started by creating a Window Based Application
in Xcode 4
that gave me the following files:spellingAppDelegate.h
, spellingAppDelegate.m
and MainWindows.xib
. I then added a new UIViewController
subclass and call it gameViewController
.
The following is my code for 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
the following is my 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;
}
I was under the impression that if i run the above code, the app will start with gameViewController.xib. However, its displaying MainWindow.xib. I know I'm probably miss something basic but I can't figure out what I did wrong. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个,它会起作用
Try this it will work
如果你这样做的话它会起作用
if you do it in this way it will work
应用程序的窗口是在 nib 文件中定义的。通常,此笔尖是
MainWindow.xib
,但您可以通过编辑应用程序的info.plist
文件来更改它。您可以在此 nib 中放置对要在启动时加载的视图控制器的引用(请参阅基于视图的应用程序模板),或者您可以在应用程序委托中使用
application:didFinishLaunchingWithOptions:
方法一样的效果。在 iOS 4.0 及更高版本上,我将使用
UIWindow
属性rootViewController
来添加视图控制器。例如:The application's window is defined in a nib file. Typically, this nib is
MainWindow.xib
, although you can change it by editing your application'sinfo.plist
file.You can place a reference to the view controller you want to load at startup in this nib (see the view-based application template), or you can use the
application:didFinishLaunchingWithOptions:
method in the application delegate to the same effect.On iOS 4.0 and later I would use the
UIWindow
propertyrootViewController
to add the view controller. For example: