开发者

UINavigationController displaying the wrong view

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.


Try this it will work

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


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜