How to call a View Controller programmatically?

前端 未结 8 2246
孤街浪徒
孤街浪徒 2020-12-02 07:04

I have looked at all the tutorials I can find on this one, and I still don\'t have the answer. I need to call another view from the code. I am using UIStoryboards

相关标签:
8条回答
  • 2020-12-02 07:42

    To create a view controller:

    UIViewController * vc = [[UIViewController alloc] init];
    

    To call a view controller (must be called from within another viewcontroller):

    [self presentViewController:vc animated:YES completion:nil];
    

    For one, use nil rather than null.


    Loading a view controller from the storyboard:

    NSString * storyboardName = @"MainStoryboard"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
    [self presentViewController:vc animated:YES completion:nil];
    

    Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.

    0 讨论(0)
  • 2020-12-02 07:45

    main logic behind this is_,

    NSString * storyboardIdentifier = @"SecondStoryBoard";
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
    
    UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];
    
    [self presentViewController:UIVC animated:YES completion:nil];
    
    0 讨论(0)
提交回复
热议问题