簡體   English   中英

使用未解決的標識符“ displayAlertMessage”

[英]Use of unresolved identifier 'displayAlertMessage'

我嘗試了一些移動代碼。 我得到的錯誤是在聲明之前使用局部變量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)

    }








    }

}

您的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)
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM