Swift 在不使用情节提要引用的情况下切换到另一个情节提要?

Swift 在不使用情节提要引用的情况下切换到另一个情节提要?,swift,xcode,Swift,Xcode,我的应用程序中有一个按钮,当用户点击时会发出警报,只有当用户点击“确定”时,我才想把它们发送到另一个故事板。是否可以在ViewController中执行此操作?没有故事板参考?代码是什么样子的?将下面的代码放在下面 // Your button tap function @IBAction func buttonClickAction(_ sender: Any) { let alert = UIAlertController(title: "", message: "Show me ne

我的应用程序中有一个按钮,当用户点击时会发出警报,只有当用户点击“确定”时,我才想把它们发送到另一个故事板。是否可以在ViewController中执行此操作?没有故事板参考?代码是什么样子的?

将下面的代码放在下面

// Your button tap function
@IBAction func buttonClickAction(_ sender: Any) {
   let alert = UIAlertController(title: "", message: "Show me next view controller", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
        self.displayNextViewController()
    }))
    alert.addAction(UIAlertAction(title: "NO", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

// Function which loads next view controller
func displayNextViewController() {
  let nextViewController = NextViewController() as UIViewController
  self.navigationController?.pushViewController(nextViewController, animated: true)
}

您可以根据自己的需求定制nextVC实例化。希望这对你有帮助

根据您评论中更新的详细信息,您需要在故事板中实例化viewController并手动执行导航,如下所示:

let alert = UIAlertController(title: "Alert!", message: "Do the thing", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (action) in
    let storyboard = UIStoryboard(name: "Bussturen", bundle: nil)
    let viewController = storyboard.instantiateViewController(identifier: "BussturenViewController") as! BussturenViewController
    //Do any more setup you might need to do for your viewController before navigation
    self.navigationController?.pushViewController(viewController, animated: true)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)

还请注意,这假设在故事板中的Identity Inspector中,您的
总线环境控制器设置为“总线环境控制器”

通过查看所有答案的帮助,我能够找到一个解决方案:

    func displayNextViewController() {

    let storyBoard : UIStoryboard = UIStoryboard(name: "Bussturen", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "BussturenViewController") as! BussturenViewController
    nextViewController.modalPresentationStyle = .fullScreen
    self.present(nextViewController, animated:false, completion:nil)
}

@IBAction func bussturenTapped(_ sender: UIButton) {


    let alert = UIAlertController(title: "", message: "Show me next view controller", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {_ in self.displayNextViewController() }))

    alert.addAction(UIAlertAction(title: "NO", style: .default, handler: nil))
    self.present(alert, animated: true, completion: nil)



}

谢谢你的回复。这段代码有一些错误。在“确定”的addAction上,我得到错误“类型为“ViewController”的值没有成员“displayNextViewController”-同样在displayNextViewController的func上,我得到此错误“属性“private”只能在非本地作用域中使用”谢谢帮助。我现在没有收到任何错误,但当单击警报上的“确定”时,它不会将我发送到其他情节提要。你知道我做错了什么吗?我想要显示的故事板名为“Bussuren.storyboard”,其viewcontroller为“BussurEnviewController.swift”。这是我的代码:谢谢你的回答,代码没有错误。然而,当我尝试运行它时,我得到了这样一个“***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:'故事板()不包含标识符为“BusstureEnviewController”的视图控制器”-我假设它与您所说的身份检查器有关?我没有完全理解你的意思。我应该在哪个情节提要中更改它?我可以消除错误,但当单击“确定”时,它仍然不会将我发送到新的情节提要。@LAMA69您实际使用的是navigationController吗?我把它作为一个假设,但如果你不是,你将需要以不同的方式呈现viewController。