How can I manually switch between UIViewControllers in storyboard?

61,671

Solution 1

I'm guessing that you are using a UINavigationController. Then you can simply do like this:

LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];

Update:

If you are using a UIStoryboard, you can set the identifier of your new viewcontroller, and then push it onto your navigationController. To set the identifier, choose your view, open the Attributes Inspector, and set the identifier ("LoginIdentifier" in my example). Then you can do this:

LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];

As a sidenote, I see that you are using capital characters for your methods. You should probably try to avoid that, and instead use lowered first-characters in your method names. And since you say you are learning Objective-C, you should check out this awesome thread here on SO: link.

Update 2:

Here is a zip file with a project showing how to do this. :-)

Solution 2

hello try to use this code

Storyboard put ID = "xxx * Name Desire" mark use StoryboarID

UIStoryboard * storyboard = self.storyboard;

DetailViewController * detail = [storyboard instantiateViewControllerWithIdentifier: @ "xxx * Name Desire"];

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

Solution 3

In this statement:

[self.view pushViewController:LoginViewController animated:YES];

it seems you are trying to push a class. You should push an object, your actual controller:

LoginViewController* controller = [[LoginViewController alloc] init...];
[self.view pushViewController:controller animated:YES];

this will at least compile, and if all the rest is fine, also give you the second controller.

EDIT:

I missed one point. You are pushing the view controller on to a view. That makes no sense, you should push the controller on to the navigation controller:

<AppDelegate> *del = (AppDelegate*)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:controller animated:YES];

This is true, at least, if you created your project from the Navigation-based template (which creates an application delegate with a reference to the navigation controller). Otherwise, please provide details about how you create the navigation controller.

Solution 4

You mentioned in a comment that you're using UIStoryboard. Are you aware of UIStoryboardSegue? All you have to do it control-drag from the button to the next view controller to establish a segue. Then you can choose the type of transition. Be aware that your view controllers need to be part of a UINavigationController in the storyboard to perform a "Push" animation.

Share:
61,671
kokoko
Author by

kokoko

wut

Updated on July 26, 2022

Comments

  • kokoko
    kokoko almost 2 years

    All I need is to view a UIView controller in same storyboard file manually with code. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView (LoginViewController) and then it goes to tab bar controller, which provides 4 UIViews. According to every UIView I have .h and .m files. I know about segue method, it is simple, but I need manual method. Maybe I am doing something wrong.

    I was trying to use this method for pushing view controller in IBAction:

    [self.view pushViewController:LoginViewController animated:YES];
    

    But it makes an error:

    Unexpected interface name ‘LoginViewController’: expected expression

    It took a lot of time to figure out what is wrong, but I had not succeed. Here is my RollEnemyController.m file:

    //  RollEnemyController.m
    #import "RollEnemyController.h"
    #import "LoginViewController.h"
    @implementation RollEnemyController;
    @synthesize AttackButtonPressed;
    
    - (IBAction)AttackButtonPressed:(id)sender {
        LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController"  bundle:nil];
        [self.view pushViewController:controller];
    }
    
    @end
    

    And this is header file:

    //  RollEnemyController.h
    
    #import <UIKit/UIKit.h>
    
    @interface RollEnemyController : UIViewController
    
    - (IBAction)RollButtonPressed:(id)sender;
    @property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;
    
    @end