2011-10-12 42 views
1

我想實現一個基本的UINavigationController,我遇到了導航控制器顯示錯誤視圖的問題。UINavigationController顯示錯誤的視圖

我開始通過創建Xcode 4這給了我下面的文件Window Based ApplicationspellingAppDelegate.hspellingAppDelegate.mMainWindows.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。我知道我可能錯過了一些基本的東西,但我無法弄清楚我做錯了什麼。謝謝。

回答

3

試試這個它會工作

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

謝謝。那工作。 – atbebtg

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

我無法讓您的代碼按原樣工作。然而,如果我刪除[self setNavigationController:newnav]的註釋;有用。 – atbebtg

+0

是的,我commneted該行,它爲我工作 – Ballu

1

在筆尖文件中定義應用程序的窗口。通常,這個筆尖是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; 
}