在不使用情节提要参考的情况下选择到另一个情节提要吗?

问题描述 投票:-1回答:3

我的应用程序中有一个按钮,单击该按钮时会发出警报,并且仅当用户单击“确定”时-我要将它们发送到另一个情节提要。是否可以在ViewController中执行此操作?没有情节提要参考?代码看起来如何?

swift xcode
3个回答
2
投票

根据评论中更新的详细信息,您将需要在情节提要中实例化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中,您已将BussturenViewController设置为“ BussturenViewController”


1
投票

放置下面的代码

// 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实例。希望这对您有所帮助!


0
投票

通过查看所有答案的帮助,我找到了解决方案:

    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)



}
© www.soinside.com 2019 - 2024. All rights reserved.