gpt4 book ai didi

ios - 当我只想从一个 Controller 到另一个 Controller 时如何设置 delegate = self

转载 作者:行者123 更新时间:2023-11-28 20:50:07 26 4
gpt4 key购买 nike

所以我有一个从 MainController() 呈现的 LoginController()。所以从技术上讲,MainController() 仍然在后台加载并位于内存中。现在在 LoginController() 中,当我按下登录按钮时,我想通知 MainController() 登录按钮已按下,因此再次获取当前用户。然后我关闭 LoginController()。

我想用协议(protocol)和委托(delegate)来做到这一点,但问题是当 MainController() 符合 LoginDelegate 时,我不知道如何将委托(delegate)设置为 self 。因为当用户注销或没有用户时,我不想回到登录 Controller ,我想回到另一个 WelcomeController()。如果我想转到 LoginController(),那么我知道我可以做到

let loginController = LoginController()
loginController.delegate = self
self.present(loginController)

但就我而言,我想使用一个完全不同的 Controller 。那么我在哪里设置委托(delegate)给自己呢?

代码如下:

//LoginController()
protocol LoginDelegate {
func didTapLogIn()
}

class LoginController: UIViewController, UITextFieldDelegate{

var loginDelegate: LoginDelegate!

override func viewDidLoad() {
super.viewDidLoad()
configureViews()
formValidation()
}

@objc func handleLogin(){
guard let email = emailTextField.text else {return}
guard let password = passwordTextField.text else {return}
Auth.auth().signIn(withEmail: email, password: password) { (auth, error) in
if let error = error{
print("There was a problem signing the user in", error.localizedDescription)
return
}
self.loginDelegate.didTapLogIn()
print("Successfully signed in the user")
self.dismiss(animated: true, completion: nil)
}
}
}

这里是符合的MainController

//MainController() which is a tab bar controller

class MainController: UITabBarController, LoginDelegate {

override func viewDidLoad() {
super.viewDidLoad()
checkLoggedInUserStatus()
}

fileprivate func checkLoggedInUserStatus(){
if Auth.auth().currentUser == nil{
DispatchQueue.main.async {
let welcomeController = UINavigationController(rootViewController: WelcomeController())
welcomeController.modalPresentationStyle = .fullScreen
self.present(welcomeController, animated: false, completion: nil)
return
}
} else {
setupTabBar()
}
}

fileprivate func setupTabBar(){
tabBar.isTranslucent = false
tabBar.barTintColor = TDGOtherBlue

let homeController = HomeController()
homeController.fetchCurrentUser()
let homeViewController = UINavigationController(rootViewController: homeController)
let chatViewController = UINavigationController(rootViewController: EditProfileController())

viewControllers = [homeViewController, chatViewController]
}

func didTapLogIn() {
checkLoggedInUserStatus()
}
}
//NOTE: ABOVE IS THE DELEGATED FUNCTION BUT WHERE DO I DO THE DELEGATE SELF DECLARATIOn

最佳答案

注意事项:

您的架构看起来像一杯鸡尾酒。但是,尽管如此,请注意:

weak var loginDelegate: LoginDelegate? // should be weak and optional

使用这个流程:

            self.dismiss(animated: true) {
self.loginDelegate.didTapLogIn()
print("Successfully signed in the user")
}

你没有描述什么是Auth

有些你不能保证

Auth.auth().currentUser 实际上不是 nil

甚至在这里:

@objc func handleLogin(){
//....
Auth.auth().signIn( ... {
///here
/// check it!
})

所以,如果不行,就用

protocol LoginDelegate {
func didTapLogIn(user: User)
}

代替

编辑

显然,如果您在关闭 LoginViewController 时没有其他信息,您可以将数据发送到 AppDelegate。

final class AppDelegate: UIResponder {
}
extension AppDelegate: LoginDelegate { ... }

要检索 AppDelegate 共享实例,您可以使用:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

第二种解决方案,您可以使用 AppDelegate 获取您的 View Controller 。这样:

let rootViewController = appDelegate.window?.rootViewController

关于ios - 当我只想从一个 Controller 到另一个 Controller 时如何设置 delegate = self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com