How can I manually switch between UIViewControllers in storyboard?

后端 未结 4 2011
你的背包
你的背包 2020-12-01 03:00

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

相关标签:
4条回答
  • 2020-12-01 03:31

    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. :-)

    0 讨论(0)
  • 2020-12-01 03:45

    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.

    0 讨论(0)
  • 2020-12-01 03:46

    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];
    
    0 讨论(0)
  • 2020-12-01 03:50

    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.

    0 讨论(0)
提交回复
热议问题