Created
February 19, 2019 08:11
-
-
Save yoheiMune/47106e5bd78fb049cbee8fa17c5101be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentViewController : UITableViewController { | |
// 省略... | |
// コメント追加ボタンが押された時. | |
@objc func onTapAddComment() { | |
// アラート表示で、コメント入力ができるようにします. | |
let alert = UIAlertController(title: "", message: "コメントを入力してください", preferredStyle: .alert) | |
// 入力欄を追加. | |
alert.addTextField { textField in | |
textField.placeholder = "コメント" | |
} | |
// 投稿ボタンを追加. | |
alert.addAction(UIAlertAction(title: "投稿する", style: .default, handler: { action in | |
// アンラップ. | |
guard | |
let post = self.post, | |
let comment = alert.textFields?[0].text else { | |
return | |
} | |
// API経由でコメント追加. | |
Api.addComment(postId: post.id, comment: comment) { errorMessage in | |
// エラーがあれば、表示して終わり. | |
if let errorMessage = errorMessage { | |
self.showAlert(message: errorMessage) | |
return | |
} | |
// 最新を読み込んで、画面に反映する. | |
self.loadCommentList() | |
} | |
})) | |
// キャンセルボタン(何もしない). | |
alert.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: { _ in })) | |
// アラート表示. | |
self.present(alert, animated: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment