views:

49

answers:

1

I have created a new application using the View Based Application Template in xCode.

My first view, which displays on loading the app, is a button based menu. One of the buttons (Make) should load a new navigation stack using a NavigationViewController. There are other buttons that when implemented will do other things outside of NavigationViewController's scope.

I would like to be able to click Make and open and display a new navigation controller.

From ViewController.m:

-(IBAction)makeStory:(id)sender{
NSLog(@"makeStory:");
navController = [[UINavigationController alloc] init];
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];

[navController pushViewController:makeStoryTableViewController animated:YES];

}

I have created a NavigationViewController in the opening ViewController.h file:

#import <UIKit/UIKit.h>
#import "MakeStoryTableViewController.h" 
@interface StoryBotViewController : UIViewController {
UINavigationController *navController;
MakeStoryTableViewController *makeStoryTableViewController;

}
- (IBAction)makeStory:(id)sender;
@end

I know I'm missing something because when I call pushViewController nothing happens - I think that somehow I have to attach the NavigationViewController to the ViewController that makeStory: is in.

For reference, my app delegate header declares the view controller in the @implementation as follows:

    UIWindow *window;
    StoryBotViewController *viewController;

with the appropriate @synthesize in the .m of the app delegate

@synthesize window;
@synthesize viewController;

How do I push a NavigationStack on from my opening view controller? Please forgive me if the question is a little vague, I'm happy to provide more information if you need it. It's my first time questioning on stackoverflow and I'm obviously a bit of a newbie with the iPhone SDK.

+1  A: 

you had it almost. But you forgot to add the NavigationController to your view.

[self.view addSubview:navController.view];

Add this in your IBAction.


EDIT:

but much likely this is not what you want, because you can't navigate back to your very first viewController. If you want to navigate back to the first viewController set up your project like in the navigation-based template. You can to this programmatically to:

Move all UINavigationController stuff from the header file of the viewcontroller to the app delegate header. And change your app delegate implementation to something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
    navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    // Add the view controller's view to the window and display.
    //    [window addSubview:viewController.view];
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
    return YES;
}

make sure to release navController in dealloc

then replace your IBAction with something like this:

- (IBAction)makeStory:(id)sender {
    makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];
    [self.navigationController pushViewController:makeStoryTableViewController animated:YES];
}

if you don't like to display the navigationbar in your first viewcontroller hide it, but make sure to unhide it if you push other items on the stack. self.navigationController.navigationBar.hidden = ...

fluchtpunkt
Great answer! Thanks. The only difference was to set the view controller as hidden I used:[[self navigationController] setNavigationBarHidden:YES animated:NO]; I had to set the MakeStoryTableViewController's viewWillDisappear as hidden again so that when I click back the navigation bar disappears again on the first view. Is there a way to do this outside of viewDidDisappear? It seems a little inefficient, tableviews that makeStoryTableViewController pushes onto the stack will have to have their navigation bar manually set to visible again.
glenstorey
To clarify, I had to set the navigation bar as hidden in makeStoryTableViewController's viewWillDisappear so that the back button preserves the initial views hidden navigation bar.
glenstorey
you could set up a UINavigationControllerDelegate. The delegate has a method named "– navigationController:willShowViewController:animated:", there you could figure out if the viewcontroller that will show is your root, and then hide the navigationBar
fluchtpunkt