我有以下警报方法 .
static func notifyUser(_ title: String, message: String) -> Void
{
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
我收到一个错误,说 presentViewController
方法中有一个额外的参数 animated
,但当我把它取出时,它仍然没有't dismiss the error, and then I' m告诉 completion
是一个额外的参数 .
6 回答
像这样在Swift 3中更改了
presentViewController
.有关详细信息,请查看Apple Documentation .
从Swift 3
completion
是可选的,所以如果你不想处理完成块,则不需要为此编写nil
,如果你想处理完成块,那么这样写 .Note: 您的notifyUser方法是使用
static
声明的,因此您不能使用self
,因此请删除它以删除更正此错误后得到的下一个错误 .斯威夫特3
您试图在静态方法中使用self,而self应该是该类的当前实例,但static不能使用self .
Without Action Handler For Alert Button
In Obj-C
In Swift 3.0
With Action Handler For Alert Button
In Obj-C
像这样调用上面的实例方法 .
In Swift 3.0
像这样调用上面的静态方法 .
Swift 3 尝试 Custom 操作取消