0

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:(UIApplication *)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

2 Answers 2

1

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;
 }
Sign up to request clarification or add additional context in comments.

9 Comments

LOL. I stepped out for 5 mins mid-answer and ended up posting the same approach Can offered. Sorry about that.
It's OK. I appreciate your comments. Thanks a lot! :D
Hi. With the two new view controllers created, what code will I put in it that I created inside the TestAppDelegate.h and TestAppDelegate.m above? Also, where will I put the IBActions code you created above? Will I just edit the code at TestAppDelegate.m or put it in the implementation files of my two new view controllers?
I'll edit my answer to give you more information. Also I suggest you check out this question I posted a great resource for beginners iPhone developers.
Also, this project is a good example of what you are trying to achieve.
|
1

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.

2 Comments

Thanks a lot Can! I appreciate it! I'll try to implement this in my app. :D
Hi. I can't seem to make it work. :( Is it possible to send me a link to a complete project instead or if you can find a nice tutorial for me to read on, can you post it here? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.