我是一个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))
}
}但这不管用。为什么?
发布于 2015-06-10 06:49:28
我注意到,当您创建一个名为“UIAlertView”的消息时,您将消息设置为message,为了演示目的,除非message是一个具有字符串值的变量,否则它将无法工作。
此外,您正在以空调用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")发布于 2015-06-10 06:21:21
试着像这样
类作为视图控制器,而不是nsobject。
class ViewController: UIViewController ,UIAlertViewDelegate{
}在情节提要中添加一个按钮,或者在按钮操作中创建yourself.Then,添加给定的代码。
var alert = UIAlertView(title: " Your Title", message: "your message", delegate: self, cancelButtonTitle: "Ok", otherButtonTitles: "Cancel")
alert.alertViewStyle = UIAlertViewStyle.Default
alert.show()然后
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
println("click",buttonIndex)
if buttonIndex == 0 {
println("ok")
}else {
println("Cancel")
}
}
}你可以根据你的需要进行定制,希望对你有帮助
发布于 2015-06-10 07:17:08
我已经用了很多次了--实际上,你也可以试试这个。
函数displayAlert(标题:字符串,消息:字符串){
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(alert, animated: true, completion: nil)
}https://stackoverflow.com/questions/30748181
复制相似问题