作者:
farcecat (WoodyLiu)
2017-05-02 22:45:46最近開始看書自學 xcode (swift)
之前沒有其他基礎,學得有點辛苦
書籍:ios 10 app 程式設計實力超進化攻略
現在學到UIAlertController
裡面有一段閉包的應用說明:
let callActionHandler = { (action:UIAlertAction!) -> Void in
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later.", preferredStyle: .alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertMessage, animated: true, completion: nil )
}
書中又教了另一種形式 (上面說推薦,比較簡潔)
但是不是用上面那個設定去介紹
let checkInAction = UIAlertAction(title: "Check in", style: .default, handler:
{
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRow(at : indexPath)
cell?.accessoryType = .checkmark
})
optionMenu.addAction(checkInAction)
我想了一個晚上一直沒有將第一種轉成第二種的形式
想請問各位該如何將第一種轉成下面那種比較簡潔的語法
上面的callActionHandler 只是(UIAlertAction)-> Void當你要設定一個UIActionAlert 時,把它當成參數傳進去例如: let callAction = UIAlertAction(title:"foo",style:.default, handler: callActionHandler)而下面的就是一般寫法我是認為上面的 alertMessage.addAction (UIAlertAction(title:style: handler: ) ,handler 應該是要寫callActionHandler 而不是nil沒看完...alertMessage 那段沒錯,上面整個closure