I have two view controllers named “view1ViewController” and “view2ViewController” in an cocoa touch project, and they are both linked to a UIView in the storyboard. Say I put a button in view1. Codewise, what’s the easiest way to switch to view2 after I press that button? What code should I need to type to get this to work?
— Update —
Despite what you have all told me about this, I still can’t get this to work. Maybe I’m not getting something? Do I need to add something else? Here is the code for view1ViewController.m:
#import "view1ViewController.h"
#import "view2ViewController.h"
@interface view1ViewController ()
@property (nonatomic, strong) view2ViewController *controller;
@end
@implementation view1ViewController
@synthesize controller = _controller;
- (view2ViewController *)controller
{
if (!_controller) {
_controller = [[view2ViewController alloc] init];
}
return _controller;
}
- (IBAction)buttonPressed {
[self.navigationController pushViewController:self.controller animated:YES];
}
@end
I haven’t touched the other files and made sure to set the class of the added view in the storyboard to view2ViewController.
Just
simple result you could be able to find yourself just googling a while…
I assumed your app is navigation-based. If this is not the case, you’ll need to init a
UINavigationController. For example in your app delegate, in yourdidFinishLaunchingWithOptions:method, you may do something likewhere
navigationControlleris aUINavigationControllerset as@property.If you do not want to setup a
UINavigationControlleryou may chose to present the other view modally using