使用未解析的标识符& displayAlertMessage'

时间:2017-11-18 01:11:50

标签: ios swift uialertcontroller

我尝试了将代码移动了一下。我得到的错误是在声明之前使用局部变量displayMessage。所以我将func displayAlertMessage移到显示屏上方,发出警告留言评论,新错误为use of unresolved identifier 'displayAlertMessage'

//
//  RegisterPageViewController.swift
//  UserLoginandRegistration
//
//  Created by Iyah Chulo on 17/11/2017.
//  Copyright © 2017 Iyah Chulo. All rights reserved.
//

import UIKit

class RegisterPageViewController: UIViewController {

    @IBOutlet weak var userEmailTextField: UITextField!
    @IBOutlet weak var userPasswordTextField: UITextField!
    @IBOutlet weak var ReenterPasswordTextField: UITextField!



    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func RegisterButtonTapped(_ sender: Any) {

        let userEmail = userEmailTextField.text;
        let userPassword = userPasswordTextField.text;
        let userReenterPassword = ReenterPasswordTextField.text;

        // Check for empty fields 
        if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
        (userReenterPassword?.isEmpty)!)

        {

            func displayAlertMessage(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)
            }

//显示警告消息             displayAlertMessage(userMessage:"所有字段都是必需的")                 返回;             }

        //Check if passwords match
        if(userPassword != userReenterPassword)
        {

// Display an alert message

    displayAlertMessage(userMessage: "Passwords do not match")

            return;

        }


        // Store data

        UserDefaults.standard.set(userEmail, forKey:"userEmail")
        UserDefaults.standard.set(userEmail, forKey:"userPassword")
        UserDefaults.standard.synchronize()

        // Display alert message with confirmation
        var 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)

    }








    }

}

1 个答案:

答案 0 :(得分:0)

您的displayAlertMessage函数定义必须超出您班级中的其他函数。

另外,请注意Swift不需要分号!

试试这个:

import UIKit

class RegisterPageViewController: UIViewController {

    @IBOutlet weak var userEmailTextField: UITextField!
    @IBOutlet weak var userPasswordTextField: UITextField!
    @IBOutlet weak var ReenterPasswordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func RegisterButtonTapped(_ sender: Any) {

        let userEmail = userEmailTextField.text
        let userPassword = userPasswordTextField.text
        let userReenterPassword = ReenterPasswordTextField.text

        // Check for empty fields
        if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
            (userReenterPassword?.isEmpty)!)
        {
            //Display alert message
            displayAlertMessage(userMessage: "All fields are required")
            return;
        }

        //Check if passwords match
        if(userPassword != userReenterPassword)
        {
            // Display an alert message
            displayAlertMessage(userMessage: "Passwords do not match")
            return;
        }

        // Store data
        UserDefaults.standard.set(userEmail, forKey:"userEmail")
        UserDefaults.standard.set(userEmail, forKey:"userPassword")
        UserDefaults.standard.synchronize()

        // Display an alert message
        displayAlertMessage(userMessage: "Registration is successful.Thank you!")
    }

    func displayAlertMessage(userMessage: String) {

        let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert)

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

        myAlert.addAction(okAction);

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

}