Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 572
photosynthesis Avatar asked Mar 02 '14 02:03

photosynthesis


1 Answers

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];
}
like image 59
Rob Jones Avatar answered Sep 21 '22 03:09

Rob Jones