这是我为在iOS7上运行而实现的控制器:
import UIKit
@objc(StartViewController)
class StartViewController: UIViewController, UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
let confirm:UIAlertView = UIAlertView(title: "sdfsdf", message: "sfsff3333", delegate: self, cancelButtonTitle: "cancel")
confirm.alertViewStyle = UIAlertViewStyle.Default
confirm.show()
}
// MARK: UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
println("buttonIndex: "+buttonIndex)
}
func alertViewCancel(alertView: UIAlertView) {
println("cancel")
}
}
事实证明,即使我将委托正确设置为自我,也不会调用UIAlertViewDelegate
中的任何函数。
当我点击警告对话框中的一个按钮时,我在控制台中获得了以下输出:
0x796d4984
如何调用UIAlertViewDelegate
方法?
编辑:我也做了:
confirm.delegate = self
这没有帮助。
答案 0 :(得分:0)
当您制作alertView
时,请confirm.delegate = self
(假设confirm
是您的UIAlertView
。
答案 1 :(得分:0)
尝试这样做。我想,原因是你没有将代表分配给自己(你的控制者)。
override func viewDidAppear(animated: Bool) {
let confirm:UIAlertView = UIAlertView(title: "sdfsdf", message: "sfsff3333", delegate: self, cancelButtonTitle: "cancel")
confirm.delegate=self;
confirm.alertViewStyle = UIAlertViewStyle.Default
confirm.show()
}
答案 2 :(得分:0)
你应该尝试iOS 8的UIAlertController和iOS 7的UIAlertView。做这样的事情:
if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
{
alert = [[UIAlertView alloc] initWithTitle:@"aa" message:@"asdadad" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"aca" message:@"asdfaf" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}