How to call a View Controller programmatically?

前端 未结 8 2250
孤街浪徒
孤街浪徒 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.

提交回复
热议问题