[英]How to call a function when a Modal View Controller has been dismissed
I currently have a modal view controller that I am using to sign in. Once the sign in is completed the modal dismisses. 我目前有一个用于登录的模态视图控制器。一旦登录完成,模态就会关闭。 I am having trouble getting a func to run on the original view controller once the modal is dismissed.
一旦模态被关闭,我就无法让func在原始视图控制器上运行。
I call the modal through a segue that is connected to a button on the main view controller. 我通过连接到主视图控制器上的按钮的segue调用模态。 The func I want to run is already in viewDidLoad and viewWillAppear where it works when the view controller is originally loaded and appears.
我要运行的功能已经在viewDidLoad和viewWillAppear中,当视图控制器最初加载并显示时它在哪里工作。
I am trying to figure out how to get it to run the func again once returned to the view dater the modal is dismissed. 我试图弄清楚如何使模态返回到视图日期后就再次运行该函子。
Original View Controller: 原始视图控制器:
class SignInView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
}
override func viewWillAppear(_ animated: Bool) {
checkUser()
}
Modal View Controller: 模态视图控制器:
@IBAction func submitBtn(_ sender: Any) {
if username.text == "" && password.text == "" {
displayAlert("Error", message: "Please Enter an Username and Password")
}
if username.text == "" {
displayAlert("Error", message: "Please Enter an Username")
}
if password.text == "" {
displayAlert("Error", message: "Please Enter a Password")
}
self.emailLogin()
}
func emailLogin() {
guard let username = username.text, let password = password.text else {
return
}
Auth.auth().signIn(withEmail: username, password: password) { (user, error) in
if error != nil {
print("Login Error")
self.displayAlert("Failed to Login", message: "Username or Password is Inccrrect")
return
} else {
print("Successfully Signed In")
self.dismiss(animated: true, completion: nil)
}
}
}
One possibility: Change 一种可能性:改变
self.dismiss(animated: true, completion: nil)
To 至
self.dismiss(animated: true) {
theSignInView.callTheMethod()
}
The protocol/delegate pattern is often used to facilitate this approach. 协议/代理模式通常用于简化此方法。
viewDidLoad
is called once when the vc is first initated , viewWillappear,viewDidAppear
are called when you dismiss a model say with overCurrentContext
/ pop from navigationController 首次启动vc时,将调用一次
viewDidLoad
viewWillappear,viewDidAppear
当您从navigationController中overCurrentContext
带有overCurrentContext
/ pop的模型时viewWillappear,viewDidAppear
将调用viewWillappear,viewDidAppear
If the above isn't your case then you need a delegate inside prepareForSegue
如果以上不是您的情况,那么您需要在
prepareForSegue
内委托
let des = segue.destination as! ModalVC
des.delegate = self
class ModelVC://
weak var delegate:MainVC?
}
Then before the dismiss use 然后在解雇使用之前
delegate?.checkUser()
self.dismiss(animated: true, completion: nil)
Also when you use viewWillAppear
don't forget super.viewWillAppear(animated)
另外,当您使用
viewWillAppear
请不要忘记super.viewWillAppear(animated)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
checkUser()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.