UIAlertController不使用Swift 3.0

Bla*_*rai 18 ios swift uialertcontroller swift3

我有以下警报方法.

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)
}
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,说是有一个额外的参数animatedpresentViewController方法,但是我把它拿出来的时候,它仍然没有清除错误,然后有人告诉我completion是一个额外的参数.

Nir*_*v D 27

presentViewController 像这样在Swift 3中被改变了.

present(alert, animated: true)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看Apple文档.

从Swift 3 completion是可选的,所以如果你不想处理完成块,不需要为此写nil,如果你想处理完成块,那么这样写.

self.present(alert, animated: true) { 

}
Run Code Online (Sandbox Code Playgroud)

注意:您的notifyUser方法是声明的,static所以你不能使用self它,所以删除它也删除你纠正这个错误后得到的下一个错误.

  • 谢啦.整个快速3让我侧身!我很感激! (3认同)

Him*_*iya 27

 let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .alert)
 let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
            //Just dismiss the action sheet
        }
 actionSheetController.addAction(cancelAction)
 self.present(actionSheetController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Gia*_*ang 11

斯威夫特3

let alertView = UIAlertController(title: "", message: "your message", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { (alert) in

})
alertView.addAction(action)
self.present(alertView, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)