UINavigationController 显示错误的视图

发布于 2024-12-09 15:24:40 字数 1972 浏览 0 评论 0原文

我正在尝试实现一个基本的 UINavigationController ,但遇到了导航控制器显示错误视图的问题。

我首先在 Xcode 4 中创建一个基于窗口的应用程序,它为我提供了以下文件:spellingAppDelegate.hspellingAppDelegate.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-12-16 15:24:40

试试这个,它会起作用

    navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
   [self.window addSubview:navigationController.view];
   [self.window makeKeyAndVisible];

Try this it will work

    navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
   [self.window addSubview:navigationController.view];
   [self.window makeKeyAndVisible];
尝蛊 2024-12-16 15:24:40

如果你这样做的话它会起作用

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];

if you do it in this way it will work

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];
墨离汐 2024-12-16 15:24:40

应用程序的窗口是在 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;
}

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's info.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 property rootViewController to add the view controller. For example:

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文