UIAlertController - Ожидание ответа пользователя - PullRequest
0 голосов
/ 11 июля 2020

код 3 будет выполнен, несмотря на отсутствие ответа. Как дождаться ответа?

         let alert = UIAlertController(title: "test", message: "Please answer: yes or not ?", preferredStyle: .alert)
                                                
            alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in alert.dismiss(animated: true, completion: nil)
                       
                // code 1...
                                                    
            }))
            alert.addAction(UIAlertAction(title: "No", style: .default, handler: { (action) in alert.dismiss(animated: true, completion: nil)
                       
                // code 2...
            
            }))

            self.present(alert, animated: true, completion: nil)

           // code 3... 

Ответы [ 3 ]

0 голосов
/ 11 июля 2020

Я понимаю, что вы хотите выполнить code3 после ответа. В этом случае поместите его в функцию (скажем, executeCode3 ()) и вызовите после кода 1 и кода 2.


let alert = UIAlertController(title: "test", message: "Please answer: yes or not ?", preferredStyle: .alert)
                                                
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in alert.dismiss(animated: true, completion: nil)
                       
    // code 1...
    //call code3  func as below
    executeCode3()
}))
alert.addAction(UIAlertAction(title: "No", style: .default, handler: { (action) in alert.dismiss(animated: true, completion: nil)
                       
    // code 2...
    //call code3  func as below
    executeCode3()
}))

self.present(alert, animated: true, completion: nil)

0 голосов
/ 11 июля 2020

Вы можете создать функцию для вывода вашего предупреждения с помощью обработчика завершения, подобного этому .. Easy to use

func showAlert(completion: @escaping (Bool) -> Void) {
 let alert = UIAlertController(title: "test", message: "Please answer: yes or not ?", preferredStyle: .alert)
          

       // add the actions (buttons)
    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: {action in 
          
         // code 1...

          completion(true)

        }))

   alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: { action in

         // code 2...

         completion(false)

       }))
    
     present(alert, animated: true, completion: nil)
}

Как использовать

showAlert { isSuccess in

  isSuccess ? print("true") : print("false")

 // isSuccess is bool having true or false accordingly 

   // code 3... here

}
0 голосов
/ 11 июля 2020

Используйте closures для параметра handler и дождитесь ответа пользователя:

func checkIfYesOrNo(handler: ((UIAlertAction) -> Void)? = nil) {
    let alert = UIAlertController(title: "test", message: "Please answer: yes or not ?", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: handler))
    alert.addAction(UIAlertAction(title: "No", style: .default, handler: handler))
    present(alert, animated: true, completion: nil)
}

Использование:

checkIfYesOrNo { action in
    if action.title == "Yes" {
        print("YES")
        // code 3...
    } else {
        print("NO")
        // code 3...
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...