0

возникла трудность с возращением значения в функции. Надо вернуть значения в функцию в зависимости от выбранной кнопки в AlertController.

func alertMessange() -> Bool{
        let alert = UIAlertController(title: "Messange", message: "", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "Cancel", style: .default){ (alert) -> Void in
//            return true
        })
        alert.addAction(UIAlertAction(title: "Ok", style: .cancel){ (alert) -> Void in
//            return false
        })
    }

Помогите пожалуйста!

0

1 ответ 1

0

В данном случае нужно не возвращать значение, а использовать closures (замыкания):

func alertMessage(after completion: @escaping (Bool) -> Void) {
    ...
    alert.addAction(UIAlertAction(title: "Cancel", style: .default){ (alert) -> Void in
        completion(true)
    })
    ...
}

И используете где нужно:

alertMessage(after: { result in 
    print(result)
})

Ваш ответ

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

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