Home  >  Q&A  >  body text

ios - 如何在一个视图控制器类中用Swift调用Storyboard

有一个标签页继承自UIViewController,这个视图控制器的界面使用storyboard实现的,如何在加载视图控制器的时候打开的是已经设计好的相应storyboard的UI界面?

如图

在这一页加载那个storyboard

伊谢尔伦伊谢尔伦2729 days ago677

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 15:47:26

    First you need to set the Storyboard ID corresponding to this vc in the storyboard, as shown in the picture

    Then you can load the object directly through this id in the code

    XXXViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"xx_id"];
    

    It can be used if
    self.storyboard is nil

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"XX_storyboard_name" bundle:[NSBundle mainBundle]];  
    XXXViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"xx_id"];
    

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:47:26

    Refer to this article: http://lvwenhan.com/ios/452.html

    The viewcontroller using StoryBoard must be initialized from the storyboard, just use this code:

    let vc = UIStoryboard(name: "Second", bundle: nil).instantiateInitialViewController() as! UIViewController
    self.navigationController?.pushViewController(vc, animated: true)

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:47:26

    First give it an ID in the storyboard, such as MainViewController.

    Then when initializing VC:

    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];  
    MainViewController *mainController = [mainStoryboard instantiateViewControllerWithIdentifier:@"MainViewController"];

    This is OC, just translate it into swift.

    reply
    0
  • Cancelreply