楼主:
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