swift 自定义 UIAlertViewDelegate 不起作用
swift custom UIAlertViewDelegate not work
我是一名 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))
}
}
但它不起作用。
为什么?
这样试试
子类为 viewcontroller ,而不是 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")
}
}
}
您可以根据需要自定义
希望对你有帮助
我注意到当您创建一个名为 "alert" 的 UIAlertView 时,您将 message 设置为 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")
其实下面这个我用过很多次了,你也可以试试这个。
func 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)
}
我是一名 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))
}
}
但它不起作用。 为什么?
这样试试
子类为 viewcontroller ,而不是 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")
}
}
}
您可以根据需要自定义 希望对你有帮助
我注意到当您创建一个名为 "alert" 的 UIAlertView 时,您将 message 设置为 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")
其实下面这个我用过很多次了,你也可以试试这个。
func 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)
}