-4

I keep receiving an error on 15th and 16th lines from the bottom. It tells me:

"use of unresolved identifier 'myAlert & okAction'"

If anyone can help, it would be much appreciated.

Code:

import UIKit

class RegisterPageViewController: UIViewController {
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var userPhoneTextField: UITextField!;
    @IBOutlet weak var userPasswordTextField: UITextField!;
    @IBOutlet weak var userConfirmPasswordTextField: UITextField!;
    @IBOutlet weak var userPlugTextField: UITextField!;

    @IBAction func RegisterButtonTapped(_ sender: Any) {
        let userName = userNameTextField.text;
        let userPhone = userPhoneTextField.text;
        let userPassword = userPasswordTextField.text;
        let userConfirmPassword = userConfirmPasswordTextField.text;
        let userPlug = userPlugTextField.text;

        // Check for empty fields
        if(userName!.isEmpty || userPhone!.isEmpty || userPassword!.isEmpty || userConfirmPassword!.isEmpty || userPlug!.isEmpty)
        {
            displayMyAlertMessage(userMessage: "All fields are required")

            return;
        }

        // check if passwords match
        if(userPassword != userConfirmPassword)
        {
         // Display an alert message
            displayMyAlertMessage(userMessage: "Passwords do not match")
            return;
        }

        // Store data
        UserDefaults.standard.set(userName, forKey: "userName")
        UserDefaults.standard.set(userName, forKey: "userPhone")
        UserDefaults.standard.set(userName, forKey: "userPassword")
        UserDefaults.standard.set(userName, forKey: "userPlug")
        UserDefaults.standard.synchronize();

        // Display alert message with confirmation
        _ = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert);

        _ = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in
            self.dismiss(animated: true, completion:nil)
        }

        myAlert.addAction(okAction);
        self.present(myAlert, animated:true, completion:nil)
    }

    func displayMyAlertMessage(userMessage:String)
    {
        let myAlert = UIAlertController(title:"Alert", message:userMessage, preferredStyle: UIAlertControllerStyle.alert);

        let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler:nil);

        myAlert.addAction(okAction);

        self.present(myAlert, animated:true, completion:nil);
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

Replace

    _ = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert);

    _ = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in
        self.dismiss(animated: true, completion:nil)
    }

with

   let myAlert  = UIAlertController(title:"Alert", message:"Registration is successful. Thank you!", preferredStyle: UIAlertControllerStyle.alert);

   let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default){ action in
        self.dismiss(animated: true, completion:nil)
    }
Sandeep P
  • 94
  • 7
0

You cannot 'see' those two objects because you are trying to reference them outside of their scope. Their scope is the function in which they are defined.

Not sure why you have those two lines at all, though, if you have

displayMyAlertMessage(userMessage:)
Daniel
  • 3,188
  • 14
  • 34