19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BlocksKitをやめてUIAlertControllerを使う

Posted at

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で振り分けると良さそうです。

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?