gpt4 book ai didi

ios - 向 alertcontroller 中的按钮添加功能

转载 作者:行者123 更新时间:2023-11-29 02:32:40 24 4
gpt4 key购买 nike

我是 swift 的新手,我在警报 View 类(class)中遵循了一些教程。我想添加功能,例如在单击警报按钮时调用另一个 View Controller ,但我不知道如何。所以请帮助我

    func showAlertController(){

var title : String = "hi!"
var message : String = NSLocalizedString("Are you feeling well? ", comment:"")
let cancelButtonTitle = NSLocalizedString("No", comment:"")
let otherButtonTitle = NSLocalizedString("Yes", comment:"")

let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
action in NSLog("No!!!!")
}

let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){
action in NSLog("welcome!!! hello back")
}

alertController.addAction(cancelAction)
alertController.addAction(otherAction)
presentViewController(alertController, animated: true, completion: nil)
}

最佳答案

试试这段代码:

let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
action in NSLog("No!!!!")
let View2 = self.storyboard?.instantiateViewControllerWithIdentifier("View2") as TwoViewController
self.navigationController?.pushViewController(View2, animated: true)

}

并选择您现有的 View Controller ,然后从下拉菜单中选择编辑>嵌入 > 导航 Controller

之后添加一个新的 View Controller 并创建一个新的 Cocoa 类并将其命名为 TwoViewController SubClass of UIViewController .

之后选择您的新 ViewController 并从 Identity Inspector 以这种方式自定义它 enter image description here

也许这可以帮到你。

关于ios - 向 alertcontroller 中的按钮添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688057/

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