カテゴリー
iOS Swift

UIAlertControllerをPresentする

@objc private func addTapped() {
  var alertTextField: UITextField?
  let alert = UIAlertController(title: "Enter New Song Title", message: nil, preferredStyle: UIAlertController.Style.alert)
  alert.addTextField(configurationHandler: { (textField: UITextField!) in
    alertTextField = textField
  })
  alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel))
  alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default) { _ in
    if let text = alertTextField?.text {
      self.dataService.addNewSong(title: text)
      self.tableView.reloadData()
    }
  })
  self.present(alert, animated: true)
}