如何创建委托以在按钮单击另一个视图控制器时更改一个视图控制器中的标签名称

时间:2017-10-13 07:28:29

标签: ios swift

这里我有两个视图控制器,并且在第二个视图控制器中点击按钮我想在第一个视图控制器中更改标签名称,这样我怎么能这样做?

第一个视图控制器,我想更改用户名标签文本

class LogInViewController: UIViewController , FBSDKLoginButtonDelegate , GIDSignInUIDelegate{
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var EnterEmail: UITextField!
    @IBOutlet weak var EnterPassword: UITextField!       

    // Login in button

    @IBAction func loginButtton(_ sender: UIButton) {

        let email = EnterEmail.text?.lowercased()
        let finalEmail = email?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        let password = EnterPassword.text

       // var name: String!
       // menuView = self.storyboard?.instantiateViewController(withIdentifier: "MenuViewController1") as! MenuViewController1

        // Register user in firebase with validations
        Auth.auth().createUser(withEmail: email!, password: password!) { (user, error) in

            if (error == nil && (finalEmail != "") && self.isValidEmail(testStr: finalEmail!)) && (password != " " && self.isPasswordValid(password!)) {
               self.displayMyAlertMessage(userMessage:"You are successfully registered ")                 
            } else {
               self.displayMyAlertMessage(userMessage:"Registration Failed.. Please Try Again !")                    
            }
        };
       // self.navigationController?.pushViewController(MenuViewController1, animated: true)  
    }
}

第二个视图控制器,点击登录按钮,想要更改第一视图控制器的标签名称

$(function() {
// Get the form.
var form = $('#ajax-inquire');

// Get the messages div.
var formMessages = $('#form-messages');


// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();

// Submit the form using AJAX.
$.ajax({
    type: 'POST',
    url: $(form).attr('action'),
    data: formData
})
.done(function(response) {
    // Make sure that the formMessages div has the 'success' class.
    formMessages.removeClass('alert-danger');
    formMessages.addClass('alert-success');

    // Set the message text.
    $(formMessages).text(response.success);

    // Clear the form.
    $('#fullName').val('');
    $('#email').val('');
    $('#telephone').val('');
    $('#message').val('');
    formMessages.show();
})
.fail(function(data) {
    // Make sure that the formMessages div has the 'error' class.
    formMessages.removeClass('alert-success');
    formMessages.addClass('alert-danger');

    // Set the message text.
    if (data.responseText !== '') {
        $(formMessages).text(data.responseText);
    } else {
        $(formMessages).text('Oops! An error occured and your inquire could not be sent.');
    }
    formMessages.show();
});
});
});

1 个答案:

答案 0 :(得分:2)

我们假设MenuViewController将被告知LoginViewController已完成登录。方式是:

首先,使用LoginViewController创建协议,并在LoginViewController中声明委托

protocol LoginViewControllerDelegate:NSObjectProtocol {
    func LoginDidFinish()
}

class LoginViewController: UIViewController{

    weak var delegate:LoginViewControllerDelegate?
    //......
}

第二次,登录完成后,调用委托方法

Auth.auth().createUser(withEmail: email!, password: password!) { (user, error) in
    if (error == nil && (finalEmail != "") && self.isValidEmail(testStr: finalEmail!)) && (password != " " && self.isPasswordValid(password!)){
        self.displayMyAlertMessage(userMessage:"You are successfully registered ")
        self.delegate?.LoginDidFinish() //Key point
    }else{
        self.displayMyAlertMessage(userMessage:"Registration Failed.. Please Try Again !")
    }
};

第三次,在MenuViewController中,在创建self时将LoginViewController指定为LoginViewController委托

let loginViewController = LoginViewController()
loginViewController.delegate = self

最后,在MenuViewController中实施委托方法,并在登录完成时调用:

//MARK: - LoginViewControllerDelegate
func LoginDidFinish() {
    print("Login did finish")
    //And change your label's text here.
}