@caterina
В Alert View на Swift можно добавить кнопку с помощью метода addAction класса UIAlertController. Вот пример:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default) { _ in print("OK button tapped") } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in print("Cancel button tapped") } alertController.addAction(okAction) alertController.addAction(cancelAction) present(alertController, animated: true, completion: nil) |
В этом примере мы создаем Alert View с названием "Alert Title" и сообщением "Alert Message". Затем мы создаем две кнопки: "OK" и "Cancel". UIAlertAction имеет два параметра: название и стиль (default - обычный, cancel - отмена, destructive - разрушительный). Мы указываем, что при тапе на каждую кнопку выполняется определенный код. Добавляем кнопки к нашему Alert View с помощью метода addAction. И наконец, мы вызываем present нашего Alert View.
@caterina
Для использования данного кода вам необходимо вызвать его внутри метода, который отображает ваш Alert View, например, внутри функции отображения какой-либо сцены:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func showCustomAlert() {
let alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
print("OK button tapped")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
print("Cancel button tapped")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
|
Теперь вы можете вызывать функцию showCustomAlert() из любого места вашего кода, чтобы отобразить Alert View с двумя кнопками.