iOS - Push viewController from code and storyboard

后端 未结 5 1211
我在风中等你
我在风中等你 2020-12-01 18:30

I have this code

 PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@\"PlaceView\"];
 [self presentViewController:newVi         


        
相关标签:
5条回答
  • 2020-12-01 19:00

    Use:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"STORYBOARD_NAME" bundle:nil];
    PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:@"PlaceView"];
    [self presentViewController:newView animated:YES completion:nil];
    

    Or:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"STORYBOARD_NAME" bundle:nil];
    PlaceViewController *newView = [storyboard instantiateViewControllerWithIdentifier:@"PlaceView"];
    [self.navigationController pushViewController:newView animated:YES];
    
    0 讨论(0)
  • 2020-12-01 19:13

    Swift 3.x

    let viewController = storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! UIViewController
    navigationController?.pushViewController(viewController, animated: true)
    
    0 讨论(0)
  • 2020-12-01 19:20

    For Storyboards, you should use performSegueWithIdentifier like so:

     [self performSegueWithIdentifier:@"identifier goes here" sender:self];
    
    0 讨论(0)
  • 2020-12-01 19:21

    By default, Xcode creates a standard view controller. we first change the view controller to navigation controller. Select the Simply select “Editor” in the menu and select “Embed in”, followed by “Navigation Controller” Step 1. Select Main story board Step 2.Click on "Editor" on top of your Xcode application. Step 3. Click on "Embed In"

    Xcode automatically embeds the View Controller with Navigation Controller.

    0 讨论(0)
  • 2020-12-01 19:25

    Objective-C:

    PlaceViewController *newView = [self.storyboard instantiateViewControllerWithIdentifier:@"storyBoardIdentifier"];
    [self.navigationController pushViewController:newView animated:YES];
    

    Please do notice below points,

    1. your storyboard identifier is correct.

    2. The root view have navigation Controller.

    Swift:

    let newView = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardIdentifier") as! PlaceViewController
    self.navigationController?.pushViewController(newView, animated: true)
    
    0 讨论(0)
提交回复
热议问题