0

I am trying to write a function that displays a UIAlertController when a UITextField is left empty. I used the following example to display the message. At this stage, I get the following error:

Use of unresolved identifier 'self'

The code I have is below.

func displayMyAlertMessage(userMessage: String){
    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
    let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
    myAlert.addAction(okAction)
    self.present(myAlert, animated: true, completion: nil)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
AltBrian
  • 2,392
  • 9
  • 29
  • 58

1 Answers1

1

The problem is probably that your func is not inside any UIViewController subclass declaration. Thus there is no self (or self is not a view controller, so there is no self.present).

matt
  • 515,959
  • 87
  • 875
  • 1,141