Ios &引用;参数“调用中的1”缺少参数;UIAlertAction处理程序(swift2.1)中出错
我试图在swift2.1中向UIAlertAction添加简单操作,但在部署项目时出现“调用中参数#1缺少参数”错误,我尝试了不同的方法并进行了大量搜索,但找不到正确的答案。 这是我的代码:Ios &引用;参数“调用中的1”缺少参数;UIAlertAction处理程序(swift2.1)中出错,ios,xcode,swift2,Ios,Xcode,Swift2,我试图在swift2.1中向UIAlertAction添加简单操作,但在部署项目时出现“调用中参数#1缺少参数”错误,我尝试了不同的方法并进行了大量搜索,但找不到正确的答案。 这是我的代码: class ViewController: UIViewController { let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert) internal fu
class ViewController: UIViewController
{
let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert)
internal func signOut()
{
self.preferences.setValue( nil , forKey: "nationalid")
}
let buttonOne = UIAlertAction(title: "sign out", style: .Default) { action in
print("Button One Pressed")
signOut() // get error
}
override func viewDidLoad() {
super.viewDidLoad()
alertController.addAction(buttonOne)
}
}
但在指定行中出现“调用中参数#1缺少参数”错误 我发现了我的愚蠢错误,我将在viewDidLoad()函数中定义我的UIAlertAction,我更改了那里的代码并解决了问题:
class ViewController: UIViewController
{
let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert)
internal func signOut()
{
self.preferences.setValue( nil , forKey: "nationalid")
}
override func viewDidLoad() {
super.viewDidLoad()
let buttonOne = UIAlertAction(title: "sign out", style: .Default) { action in
print("Button One Pressed")
signOut()
}
alertController.addAction(buttonOne)
}
}
您已经在viewDidLoad或函数中编写了此函数 例如:
override func viewDidLoad() {
super.viewDidLoad()
let alertController = UIAlertController()
let OKAction = UIAlertAction(title: "Sign out", style: .Default) { (action:UIAlertAction!) in
signOut()
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion:nil)
}
希望这会有所帮助您还需要添加presentViewController来显示它。