2011-10-12 5 views
1

J'essaie d'implémenter un UINavigationController de base et je rencontre un problème avec le contrôleur de navigation affichant une mauvaise vue.UINavigationController affichant la mauvaise vue

J'ai commencé par la création d'un Window Based Application dans Xcode 4 qui m'a donné les fichiers suivants: spellingAppDelegate.h, spellingAppDelegate.m et MainWindows.xib. J'ai ensuite ajouté une nouvelle sous-classe UIViewController et l'appeler gameViewController.

Ce qui suit est mon code pour 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 

ce qui suit est mon 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; 
} 

J'avais l'impression que si je lance le code ci-dessus, l'application commencera par gameViewController.xib. Cependant, il affiche MainWindow.xib. Je sais que je manque probablement quelque chose de basique mais je n'arrive pas à comprendre ce que j'ai fait de mal. Je vous remercie.

Répondre

3

Essayez cela, il travaillera

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

merci. ça a marché. – atbebtg

1

si vous le faites de cette façon, il travaillera

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

Je ne pouvais pas obtenir votre code pour fonctionner tel quel. Cependant, si je supprime le commentaire pour [self setNavigationController: newnav]; Ça marche. – atbebtg

+0

oui j'ai commneted cette ligne et il est travaillé pour moi – Ballu

1

la fenêtre de l'application est définie dans un fichier nib. En règle générale, cette plume est MainWindow.xib, mais vous pouvez le modifier en éditant le fichier info.plist de votre application.

Vous pouvez placer une référence au contrôleur de vue que vous voulez charger au démarrage dans cette pointe (voir le modèle d'application basé sur la vue) ou vous pouvez utiliser la méthode application:didFinishLaunchingWithOptions: dans le délégué d'application dans le même effet.

Sur iOS 4.0 et versions ultérieures, j'utiliserais la propriété UIWindowrootViewController pour ajouter le contrôleur de vue. Par exemple:

- (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; 
} 
Questions connexes