每次启动视图控制器时显示UIAlertController

时间:2018-06-22 16:54:59

标签: ios swift uialertcontroller

当前,当用户点击HeaderButton时,UIAlertController出现。我试图使UIAlertController每次视图控制器最初启动时自动出现。有什么建议吗?

// MARK: - RestaurantListTableViewHeaderDelegate
extension RestaurantListViewController: RestaurantListTableViewHeaderDelegate {
  func didTapHeaderButton(_ headerView: RestaurantListTableViewHeader) {
    let locationPicker = UIAlertController(title: "Select location", message: nil, preferredStyle: .actionSheet)
    for location in RestaurantListViewController.locations {
      locationPicker.addAction(UIAlertAction(title: location, style: .default) { [weak self] action in
        guard let `self` = self else { return }
        self.currentLocation = action.title
        self.tableView.reloadData()
      })
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    locationPicker.addAction(cancelAction)
    present(locationPicker, animated: true)
  }
}

2 个答案:

答案 0 :(得分:0)

这不是一个优雅的解决方案,但可以使用:

var alertAlreadyShown = false

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  if !alertAlreadyShown {
      alertAlreadyShown = true
      /* Code for showing alert */
  }
}

答案 1 :(得分:0)

我保留了标题按钮被点击时的扩展名,并在viewDidLoad中添加了以下内容:

super