SCLAlertView-Swiftがサクッと使うのにいい感じです。
簡単ですが、使用例です。サンプルアプリにボタン3つおいておきます。
showSuccessで成功時のアラートが、showErrorでエラーじのアラートがそれぞれ出せます。他にも
- showWarning
- showInfo
- showNotice
があります。
showSuccess
1@IBAction func showSuccess(_ sender: Any) {
2 SCLAlertView().showSuccess("サクセス", subTitle: "保存しました", closeButtonTitle: "閉じる")
3}
showError
closeButtonTitleを指定しないとボタンのラベルはDoneとなります。
1@IBAction func showError(_ sender: Any) {
2 SCLAlertView().showError("エラー", subTitle: "")
3}
独自ボタン
独自のボタンを追加して個別の処理を割り当てることができます。closeボタンには直接処理を割り当てられないので、何もせずに閉じる用途がない場合にはshowCloseButton: falseなapperanceを使ってアラートを生成します。
1@IBAction func showConfirm(_ sender: Any) {
2 let appearance = SCLAlertView.SCLAppearance(
3 showCloseButton: false
4 )
5 let alert = SCLAlertView(appearance: appearance)
6 alert.addButton("Cancel") {
7 print("cancel")
8 }
9 alert.addButton("OK") {
10 print("ok")
11 }
12 alert.showInfo("確認", subTitle: "よろしいですか?")
13}