首页 文章

如何从课程中以编程方式加载故事板?

提问于
浏览
156

我的问题是我正在寻找使用 storyboardxib 的方法 . 但我可以很难在storyboard中嵌套所有xib文件 . 所以我在代码中寻找一种方法,比如使用 alloc, init, push for viewControllers . 在我的情况下,我在故事板中只有一个控制器: UITableViewController ,它有静态单元格,其中包含我想要显示的一些内容 . 如果有人知道在没有大量重构的情况下使用xib和storyboard工作的正确方法,我将非常感谢您的帮助 .

7 回答

  • 56

    在故事板中,转到“属性”检查器并设置视图控制器的标识符 . 然后,您可以使用以下代码显示该视图控制器 .

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];
    
  • 14

    试试这个

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
    
    [[UIApplication sharedApplication].keyWindow setRootViewController:vc];
    
  • 334

    在迅速
    NavigationControllerpushController 你可以替换

    present(vc, animated:true , completion: nil)
    
  • 1

    您始终可以直接跳转到根控制器:

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [storyboard instantiateInitialViewController];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];
    
  • 2

    斯威夫特3

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "viewController")
    self.navigationController!.pushViewController(vc, animated: true)
    

    斯威夫特2

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")
    self.navigationController!.pushViewController(vc, animated: true)
    

    Prerequisite

    将故事板ID分配给视图控制器 .

    Storyboard ID

    IB>显示身份检查器>身份>故事板ID

    Swift (legacy)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController
    self.navigationController!.pushViewController(vc!, animated: true)
    

    编辑:在Fred A.的评论中建议使用Swift 2

  • 3

    在属性检查器中,为该视图控制器提供标识符,以下代码适用于我

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
  • 0

    对于 swift 3 and 4 ,您可以这样做 . 好的做法是将Storyboard的名称设置为StoryboardID .

    enum StoryBoardName{
       case second = "SecondViewController"
    }
    extension UIStoryBoard{
       class func load(_ storyboard: StoryBoardName) -> UIViewController{
          return UIStoryboard(name: storyboard.rawValue, bundle: nil).instantiateViewController(withIdentifier: storyboard.rawValue)
       }
    }
    

    然后你可以在你的ViewController中加载你的Storyboard,如下所示:

    class MyViewController: UIViewController{
         override func viewDidLoad() {
            super.viewDidLoad()
            guard let vc = UIStoryboard.load(.second) as? SecondViewController else {return}
            self.present(vc, animated: true, completion: nil)
         }
    

    }

    当您创建新的Storyboard时,只需在StoryboardID上设置相同的名称,并在枚举“ StoryBoardName ”中添加Storyboard名称

相关问题