这里我有两个视图控制器,并且在第二个视图控制器中点击按钮我想在第一个视图控制器中更改标签名称,这样我怎么能这样做?
第一个视图控制器,我想更改用户名标签文本
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();
});
});
});
答案 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.
}