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.
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With