0

Всем привет! Такая ситуация -

У меня UIViewController, который презентуется модально (present modally). В конце всех мне нужных действий появляется UIAlertController. После его появления, либо же после нажатия на кнопку "OK", я хочу закрыть этот UIViewController. Метод self.dismiss(animated: true, completion: nil) срабатывает, но срабатывает он именно на сам UIAlertController .

Допустим, вот метод -

func showAlertAndDismiss() {
savePost(previewPost: previewPost!, completion: { (success) in
      if success {

       let alertOK = UIAlertController(title: "Пост опубликован", message: nil, preferredStyle: UIAlertController.Style.alert)
       let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
       alertOK.addAction(okAction)

       self.present(alertOK, animated: true, completion: nil)
       self.dismiss(animated: true, completion: nil)

}

Закрывается только сам AlertController, а не UIViewController В хендлере UIAlertAction также пробовал вставлять self.dismiss, но убирается только алерт. Как быть?

Мне не важно когда UIViewController закроется - после появления UIAlertController, или после нажатия на кнопку OK. Главное закрыть ViewController

2 ответа 2

1

Быстро набросал, но все работает, как надо. Проверь, где у тебя ошибка. Вот первый ВьюКонтроллер

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let button = UIButton(frame: CGRect(x: 50, y: 250, width: 150, height: 50))
        button.addTarget(self, action: #selector(presentViewController), for: .touchUpInside)
        button.backgroundColor = .green
        view.addSubview(button)
    }

    @objc private func presentViewController() {
        let viewController = SecondViewController()
        present(viewController, animated: true)
    }
}

Вот второй ВьюКонтроллер.

final class SecondViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        let button = UIButton(frame: CGRect(x: 50, y: 250, width: 150, height: 50))
        button.addTarget(self, action: #selector(presentViewController), for: .touchUpInside)
        button.backgroundColor = .green
        view.addSubview(button)
    }

    @objc private func presentViewController() {
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default) { _ in
            self.dismiss(animated: true)
        }
        alertController.addAction(action)
        present(alertController, animated: true)
    }
}

Никаких сторибордов и других сетапов.

1
  • Привет! Огромное спасибо за ответ! Вот что заметил - self.dismiss(animated: true) у меня просто не выполняется. Брейк-поинт не срабатывает
    – Gatsby
    11 янв 2019 в 6:28
0

Очень просто по нажатию на ОК

let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) { (UIAlertAction) in
     self.dismiss(animated: true, completion: nil)
}
3
  • Я писал выше, что в хендлере я уже прописывал так. Он закрывает только UIAlertController
    – Gatsby
    9 янв 2019 в 17:35
  • @Gatsby у меня срабатывает, специально смоделировал вашу ситуацию
    – schmidt9
    9 янв 2019 в 17:45
  • @Gatsby на какой версии iOS вы тестировали?
    – schmidt9
    9 янв 2019 в 17:48

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.