使用 viewDidAppear() 在 swift5 中关闭另一个 ViewController 后刷新 ViewController

Refreshing ViewController after dismissing another ViewController in swift5 using viewDidAppear()

我试图在用户登录时关闭我的 loginVC 我只是让用户签名

if success {
dismiss(animated:true, completion:nil)
}

关闭后显示我的主页是 AuthVC 另一个 viewController。

override func viewDidAppear(_ animated: Bool) {
                    super.viewDidAppear(animated)
                   
                    if Auth.auth().currentUser != nil {
                        self.dismiss(animated: true, completion: nil)
                    }
            

class LoginVC: UIViewController {
    
    @IBOutlet weak var emailField: InsetTxtField!
    
    @IBOutlet weak var pwdField: InsetTxtField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        emailField.delegate = self
        pwdField.delegate = self
        
    }
    @IBAction func signInBtnWasPressed(_ sender: Any) {
        if emailField.text != nil && pwdField.text != nil {
            AuthService.instance.loginUser(email: emailField.text!, andPassword: pwdField.text!) { (success, loginError) in
                if success {
                    print("login sucessfully")
                    self.dismiss(animated: true, completion: nil)
                    
                } else {
                    print(loginError!.localizedDescription)
                    
                }
                AuthService.instance.registerUser(email: self.emailField.text!, andPassword: self.pwdField.text!) { (success, registrationError) in
                    if success {
                        AuthService.instance.loginUser(email: self.emailField.text!, andPassword: self.pwdField.text!) { (success, nil) in
                             print("Successfully registered user")
                            self.dismiss(animated: true, completion: nil)
                        
                    }
                    }
                    else {
                        print(String(describing: registrationError?.localizedDescription))
                    }
                }
            }
            
        }
    }
     @IBAction func closeBtnWasPRessed(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
}

希望现在一切都清楚了。 我的 viewDidAppear 函数没有被调用。

模态视图控制器将 UIModalPresentationOverFullScreen 作为模态呈现样式

父视图永远不会消失,所以它不会调用viewDidAppear。

为了解决这个问题,post 在关闭模态视图并在父视图控制器上处理它时在 NSNotificationCenter 上显示一条消息。

您可以将 modalPresentationStyle LoginVC 更改为 .fullScreen

viewController.modalPresentationStyle = .fullScreen

示例:

let nav = UINavigationController(rootViewController: LoginVC())
nav.modalPresentationStyle = .fullScreen
present(nav, animated: true, completion: nil) // if you add navigationController

or 

let vc = LoginVC()
vc.modalPresentationStyle = .fullScreen
present(nav, animated: true, completion: nil)