photosynthesis
photosynthesis

Reputation: 2890

How to add an UINavigationController to an existing UIViewController programmatically

I am newbie to iOS programming, and this problem may seem stupid to someone, I am sorry. I have a problem here is that I have an existing UIViewController (A), and another UIViewController (B), I want to add an UINavigationController to A such that when I press a button on A, I will be lead to B. I tried the following approach, but it does not solve the problem:

in AppDelegate:

@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.tabbarController = [[UITabBarController alloc] init];
    UIViewController *viewController_1 = [[WUSTLViewController_1 alloc] init];
    UIViewController *viewController_2 = [[WUSTLViewController_2 alloc] init];

    UIViewController *viewController_3 = [[WUSTLViewController_3 alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController: viewController_3];

    self.tabbarController.viewControllers = [NSArray arrayWithObjects:viewController_1, viewController_2, navigationController, nil];
    self.window.rootViewController = self.tabbarController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

In WUSTLViewController_3.h (A):

#import <UIKit/UIKit.h>
#import "WUSTLViewController_4.h"

@interface WUSTLViewController_3 : UIViewController {
    WUSTLViewController_4 *viewController_4;
}

@property UINavigationController *navigationController;

@end

In WUSTLViewController_3.m (A):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    viewController_4 = [[WUSTLViewController_4 alloc] init];

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(80, 50, 150, 40);
    [myButton setTitle:@"Push" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
}

- (IBAction)pressButton:(id)sender
{
    [self.navigationController pushViewController:viewController_4 animated:YES];
}

When I click the button, nothing happened.

Upvotes: 6

Views: 28430

Answers (2)

Rob Jones
Rob Jones

Reputation: 4985

This is not how UINavigationControllers work. You need to set A as the rootViewController of your UINavigationController. UINavigationController is a container for other view controllers.

For example, in your AppDelegate you might do something like this:

UIViewController *A = [[UIViewController alloc] init];    
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:A];
UIViewController *X = [[UIViewController alloc] init];
UIViewController *Y = [[UIViewController alloc] init];
UIViewController *Z = [[UIViewController alloc] init];
UITabBarController *tabVC = [[UITabBarController alloc] init];
tabVC.viewControllers = @[X, Y, Z, navVC];
self.window.rootViewController = tabVC;

And in A

- (IBAction)pressButton:(id)sender
{
    [self.navigationController pushViewController:B animated:YES];
}

Upvotes: 10

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

The problem is A needs to be in a navigation controller before you can push B.

If A is already in a navigation controller:

[A.navigationController pushViewController:B animated:YES];

If A is not in a navigation controller, then you need to think about how you want your app's views laid out.

  1. Put A in a navigation controller.
  2. Present a navigation controller from A as a presented view controller.

If you go for option 1, then by the time A is loaded, it will already be in a navigation controller. You only need to push B.

[A.navigationController pushViewController:B animated:YES];

If you go for option 2, then you need to present the navigation controller from A.

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:B];
[A presentViewController:navigationController animated:YES completion:NULL];

NOTE: This will create a new navigation stack with B as the root. You will need to have some way to dismiss the navigation controller, when you are done.

[A dismissViewControllerAnimated:YES completion:NULL];

If you want to dismiss the navigation controller from within B, then you need to do the following.

[B.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];

That is from B, get the navigation controller, and from the navigation controller, get the presenting view controller (which is A).

Upvotes: 1

Related Questions