Ios 在Swift 4中单击按钮时,如何以编程方式启动不同的故事板?

Ios 在Swift 4中单击按钮时,如何以编程方式启动不同的故事板?,ios,swift,Ios,Swift,这是到目前为止我的代码 joinButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) } @objc func buttonAction(_sender: UIButton) { print("I want to go to a storyboard here") } 我已经编写了所有代码,但从未使用过故事板,但现在我想手动使用UI。如何手动玩故事板?或者甚至渲染到不同的viewco

这是到目前为止我的代码

joinButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
 @objc func buttonAction(_sender: UIButton) {
    print("I want to go to a storyboard here")
 }
我已经编写了所有代码,但从未使用过故事板,但现在我想手动使用UI。如何手动玩故事板?或者甚至渲染到不同的viewcontroller?谢谢

您需要为要以编程方式使用的视图控制器指定情节提要ID:

选择故事板 去找身份检查员⌥⌘3. 分配情节提要ID 如果要在情节提要中添加手动分段,请注意:您可以有多个手动分段:

选择故事板 选择源代码视图控制器 转到连接检查器⌥⌘6. 在触发的序列中,将手动旁边的点拖动到目标视图控制器,然后选择所需的方法 在文档大纲中,选择显示顺序。。。然后转到属性检查器⌥⌘4并分配一个标识符 在按钮功能中,您只需通过以下方式使用segue:

self.performSegue(withIdentifier: "<IdentifierForSegue>", sender: self)
或者,如果您不希望使用手动序列:

// If view controllers are in the same Storyboard
let storyboard = self.storyboard!
// If view controllers are in different Storyboards
// let storyboard = UIStoryboard.init(name: "<NameOfYourStoryboard>", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<IdentifierForTargetViewController>")
// If you don't have a navigation controller
self.present(viewController, animated: true, completion: nil)
// If you have a navigation controller
// self.navigationController?.pushViewController(viewController, animated: true)

您可以通过以下方式编程完成:

斯威夫特3+


我似乎无法将其拖动到我的视图控制器。请记住,这是我唯一的故事板,其他一切都是用代码编写的。我是不是让赛格自己去了?我不明白:假设你有两个视图控制器A和B,如果你想从A转到B,然后选择故事板并选择视图控制器A和to go Connections Inspector,你应该在顶部看到触发的片段,手动旁边应该有一个点,将其拖动到视图控制器B。由于我以编程方式创建了collectionviewcontroller,因此我只有一个视图控制器,因此我甚至无法将a连接到B。只需按照最后一步,使用您创建的collectionviewcontroller调用present或pushViewController,感谢!!我被困在这上面了。这段代码也有道理:!欢迎@zyne
   let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
   let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerID") as UIViewController
   present(vc, animated: true, completion: nil)