开发者

How to use multiple custom View Controllers with a Navigation Controller?

Basically, I have two view controllers inside the MainWindow.xib that can be viewed by clicking the Bar Button in my Navigation Controller. I wanted those two view controllers to be separated from the MainWindow.xib with their own header, implementation and xib files and still make Navigation Controller inside of MainWindow.xib work in them.

To better understand it, please see the codes below:

Thanks a lot!

TestAppDelegate.h

#import <UIKit/UIKit.h>

    @interface TestAppDelegate : NSObject <UIApplicationDelegate>
    {
        //Navigation Controller
        IBOutlet UINavigationController *navigationController;


        //View Controllers
        UIViewController *viewController;
        UIViewController *viewController2;
        UIViewController *viewController3;

    }

    @property (nonatomic, retain) IBOutlet UIWindow *window;

    //Navigation Controller
    @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;


    //View Controllers
    @property (nonatomic, retain) IBOutlet UIViewController *viewController;
    @property (nonatomic, retain) IBOutlet UIViewController *viewController2;
    @property (nonatomic, retain) IBOutlet UIViewController *viewController3;


    - (IBAction)next;
    - (IBAction)next2;


    @end

TestAppDelegate.m

#import "TestAppDelegate.h"

@implementation TestAppDelegate

@synthesize window = _window;

//Navigation Controller
@synthesize navigationController;

//View Controllers
@synthesize viewController;
@synthesize viewController2;
@synthesize viewController3;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    //Navigation Controller
    [self.window addSubview:[navigationController view]];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplicati开发者_运维知识库on *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

- (void)dealloc
{

    [viewController release];
    [viewController2 release];
    [viewController3 release];

    [navigationController release];
    [_window release];
    [super dealloc];
}


- (IBAction)next {
    [navigationController pushViewController:viewController2 animated:YES];
}


- (IBAction)next2 {
    [navigationController pushViewController:viewController3 animated:YES];
}


@end

Inside MainWindow.xib:

http://i52.tinypic.com/10xa45f.png


I normally don't touch MainWindow.xib. I suggest the following:

  1. Create a MainController which will be your MainView that subclass UIViewController by going to File > New > New File. That will create a .h/.m and nib file for each ViewController. There add whatever UI you want for your app. For Example, add two buttons and wire those buttons to IBActions in your MainController. This should be declared and implemented in your MainController.{h/m}, respectively.

  2. After that create another two ViewControllers, the same way.

  3. The body of those IBActions should create an instance of the your ViewControllers and then push them.

It would look something like this:

YourViewController *yvc = [[YourViewController alloc] init];
[self.navigationController pushViewController:yvc animated:YES];
[yvc release];

Finally, you have to push the MainController in your AppDelegate and add your NavigationController to the view.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     MainViewController *mvc = [[MainViewController alloc] init];
     UINavigationController *unvc = [[UINavigationController alloc] init];
     [unvc pushViewController:mvc animated:NO];
     [mvc release];
     [self.window addSubview:unvc.view];
     [self.window makeKeyAndVisible];
     return YES;
 }


Set the "class" property in Interface Builder (third tab in the inspector, "Custom Class") to the name of the custom class you're planning to use, and then put the name of the .xib file you want to load from in the "NIB Name" (fourth tab).

The code you're using to push the viewController is alright. Make sure to never accidentaly dealloc any of the two UIViewController.

Speaking of which, keep in mind that this approach is keeping the ViewControllers always in memory, even when not in use. Another approach is to completely remove the IBOutlets for the two ViewControllers, and do something like:

- (IBAction)next
{
    MyCustomViewController *customViewController = [[MyCustomViewController alloc] initWithNibName:<#NibName or nil#> bundle:[NSBundle mainBundle]]; 
    [navigationController pushViewController:customViewController animated:YES];
    [customViewController release];
}

This creates the object when needed (If I remember right, the UI elements from the xib are cached somewhere, so that may be irrelevant[citation needed]). Just something to keep in mind, depending on the frequency of use of your two ViewControllers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜