gpt4 book ai didi

快速自定义 UIAlertViewDelegate 不起作用

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

我是一名 Android 开发人员。

我想在我的代码中使用自定义 UIAlertViewDelegate

类似演示

import UIKit

class ViewController: UIViewController,UIAlertViewDelegate {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
println(buttonIndex)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func onClick1(sender: UIButton) {

// define here not work
let delegate = MyUIAlertViewDelegate()

let alert = UIAlertView(title: "title", message: "message", delegate: delegate, cancelButtonTitle: "cancel", otherButtonTitles: "other","hello")

alert.alertViewStyle = UIAlertViewStyle.Default

alert.show()

}
}

// define here works
// let delegate = MyUIAlertViewDelegate()


class MyUIAlertViewDelegate : UIViewController, UIAlertViewDelegate {
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
println("click" + String(buttonIndex))
}
}

但它不起作用。为什么?

最佳答案

我注意到,当您创建一个名为“alert”的 UIAlertView 时,您将消息设置为消息,出于演示目的,除非消息是具有字符串值的变量,否则这将不起作用。

而且你什么也没有调用alert.show()!它需要被某些东西或某个地方调用,所以在我下面完成的示例中它位于 viewDidAppear 中。

如果您需要此代码完美运行,请复制并粘贴此代码,如有任何问题,请随时提出。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
alert.show()
}

}

class MyUIAlertViewDelegate : NSObject, UIAlertViewDelegate {
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
print("click" + String(buttonIndex))
}
}

let delegate = MyUIAlertViewDelegate()
let alert = UIAlertView(title: "warning", message: "This is a message", delegate: delegate, cancelButtonTitle: "Cancel")

关于快速自定义 UIAlertViewDelegate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30748181/

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