UIAlertViewとUIActionSheetでBlocksKitを使っていたコードを、iOS8から使えるようになったUIAlertControllerに置き換えます。
UIAlertView/UIActionSheetを直接使っている場合に比べると、BlocksKitを使っている場合は大体同じようなコードなので移行が簡単です。
UIAlertView+BlocksKitの置換
UIAlertView+BlocksKit
func showConfirm() {
let alert = UIAlertView.bk_alertViewWithTitle("", message:"Are you sure?") as UIAlertView
alert.bk_addButtonWithTitle("OK", handler:{
...
})
alert.bk_setCancelButtonWithTitle("Cancel", handler:{
...
})
alert.show()
}
UIAlertController(Alert)
func showConfirm() {
let alert = UIAlertController(title:"", message:"Are you sure?",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"OK", style:UIAlertActionStyle.Default,
handler:{ action in
...
}))
alert.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel,
handler:{ action in
...
}))
self.presentViewController(alert, animated:true, completion:{})
}
UIActionSheet+BlocksKitの置換
UIActionSheet+BlocksKit
func showConfirm() {
let sheet = UIActionSheet.bk_actionSheetWithTitle("Are you sure?") as UIActionSheet
sheet.bk_addButtonWithTitle("OK", handler:{
...
})
sheet.bk_setCancelButtonWithTitle("Cancel", handler:{
...
})
sheet.showInView(self.view, animated:true)
}
UIAlertController(ActionSheet)
func showConfirm() {
let alert = UIAlertController(title:"", message:"Are you sure?",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"OK", style:UIAlertActionStyle.Default,
handler:{ action in
...
}))
alert.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel,
handler:{ action in
...
}))
self.presentViewController(alert, animated:true, completion:{})
}
感想
UIAlertControllerStyleをAlertかActionSheetか切替えるだけでよくなったのがいいですね。
iOS7以前をサポートする場合はラッパークラスを定義して、内部でBlocksKitとUIAlertControllerで振り分けると良さそうです。