gpt4 book ai didi

swift - UIAlertController 按钮单击导航到 NavigationController 的第一个 ViewController

转载 作者:行者123 更新时间:2023-11-30 10:18:35 25 4
gpt4 key购买 nike

我是 swift 和 iOS 编程新手。我有 StartViewController,它有一个按钮,当单击带有两个按钮的 UIAlertController 时 - 拒绝接受。当点击接受时,我想导航到MyNavigationController的 - ViewControllerMyNavigationController 有四个 ViewController,我使用滑动菜单导航到它们。

我附上了示例代码和 Storyboard的屏幕截图以供引用。

 @IBAction func showAlert() {

let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)


let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)

let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

//I think the code to navigate should go here, help please.

}

alertController.addAction(acceptAction)

presentViewController(alertController, animated: true, completion: nil)

}

Storyboard屏幕截图 -

enter image description here

在上面的屏幕截图中,登录按钮将打开一个 UIAlertController alertController。此 AlertController 上的“Accept”按钮应导航至“MyNavigationController”上的“ViewController”。

这个MyNavigationController还有另外三个ViewController,它们使用滑动菜单进行导航。

提前谢谢您。

最佳答案

您需要在当前 View Controller 和目标 View Controller 之间创建一个segue,并确保segue ID与您的performSegueWithIdentifier调用中的id匹配

@IBAction func showAlert() {

let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)

let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier
}

alertController.addAction(declineAction)
alertController.addAction(acceptAction)

presentViewController(alertController, animated: true, completion: nil)
}

关于swift - UIAlertController 按钮单击导航到 NavigationController 的第一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com