Skip to content

Instantly share code, notes, and snippets.

@yoheiMune
Created February 19, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoheiMune/47106e5bd78fb049cbee8fa17c5101be to your computer and use it in GitHub Desktop.
Save yoheiMune/47106e5bd78fb049cbee8fa17c5101be to your computer and use it in GitHub Desktop.
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