uiViewController Swift IOS
-
02-01-2020 - |
Pergunta
Eu estou usando swift e tenho uma classe chamada ViewController() que é vinculada ao meu storyboard.Estou tentando obter um modo de exibição de alerta para exibição a partir de outra classe.Eu estou usando o seguinte código no entanto, ele não irá abrir uma caixa de alerta.Qualquer Idéias?
public class SomeClass {
func showAlert(title:String, body:String) {
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
}
Solução
func showAlert(title:String, body:String) {
var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
var vc: ViewController = ViewController()
vc.presentViewController(alert, animated: true, completion: nil)
}
Outras dicas
Você tem que chamar presentViewController
no seu UIViewController
e passar na sua UIAlertViewController
.
Em UIViewController:
auto.presentViewController(alerta, animado:verdadeiras, a conclusão:nil)
alert
sendo o seu UIAlertViewController
.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow