简体   繁体   中英

How do I switch views in cocoa touch?

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

[self.navigationController pushViewController:view2ViewController animated:YES];

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 your didFinishLaunchingWithOptions: method, you may do something like

navigationController = [[UINavigationController alloc] init];
[self.window addSubview:navigationController.view];
[navigationController pushViewController:self.viewController animated:NO];

[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

where navigationController is a UINavigationController set as @property .

If you do not want to setup a UINavigationController you may chose to present the other view modally using

[self presentModalViewController:view2ViewController animated:YES];

If you have not setup a navigation controller, this approach will not work. To setup the navigation controller, you need to make sure that the current view is the root view controller for the navigation controller. This can be done programmatically, but if your using the Storyboard it would be much easier to embed the view in a navigation controller. Once the controller is in place, the pushing should work.

For the Storyboard embedding, go to the top menu and choose Edit->Embed in Navigation Controller.

For doing it through the code, wherever you create this view, instead write it as

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:view1ViewController];

Then add the navigation controller wherever you added the view1ViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM