开发者

iOS - loading navigation controller via code

开发者 https://www.devze.com 2023-03-08 16:18 出处:网络
I have a navigation controller that was setup via the project wizard.Currently when the application is launched the navigation controller automatically gets created and displayed.

I have a navigation controller that was setup via the project wizard. Currently when the application is launched the navigation controller automatically gets created and displayed.

I now need to control the display of the navigation controller via code instead of via the .xib magic. How do I disable the automatic creation of the MainWindow.xib/RootViewController.xib? I confess I don't actually know what's going on and the relationship between MainWindow.xib and RootController.xib as the wizard set all this up.

Any refere开发者_运维百科nces or code snippets on this would be helpful.. thanks!


To create the root navigation controller without a nib:

In your App Delegate you should see the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.

    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

self.navigationController refers to the navigation controller that was loaded from MainWindow.xib (the name of this file is specified in your app's Info.plist file; see below).

Open MainWindow.xib and disconnect the navigationController property of your App Delegate, then delete the Navigation Controller (not the Window) object in the Objects palette.

Remove the IBOutlet property from the navigationController @property declaration in your App Delegate's header file (since it will no longer be wired from a nib file).

Replace the code in your App Delegate with something along the following lines:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];

    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

To create the main window without a nib:

You probably don't need to do this (and I don't recommend it), but since you (sort of) asked...

Delete MainWindow.xib.

In main.m, replace the last argument to UIApplicationMain with the name of your App Delegate (with no extension). For instance:

int retVal = UIApplicationMain(argc, argv, nil, @"TestProjectAppDelegate");

Open your Info.plist file and delete the following two lines:

<key>NSMainNibFile</key>
<string>MainWindow</string>

Remove the IBOutlet property from the window @property declaration in your App Delegate's header file.

Create the window in your App Delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // The rest stays the same...
}
0

精彩评论

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