简体   繁体   English

模态视图控制器被关闭后如何调用函数

[英]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.

相关问题 iOS - 了解模态视图何时被解除 - iOS - Know when modal view has been dismissed 视图控制器被解雇后如何显示警报 - How to present an alert after view controller has been dismissed 如何检查视图 controller 是否已在 Swift 中被取消 - How to check if a view controller has been dismissed in Swift 关闭时如何将数据从模态视图控制器传回 - How to pass data from modal view controller back when dismissed 取消模态视图时VC中的触发功能 - Trigger function in VC when modal view is dismissed ECSlidingViewController中的Top View Controller是否有办法知道边栏菜单何时被关闭? - Is there a way for the Top View Controller in ECSlidingViewController to know when the sidebar menu has been dismissed? 如果当前视图控制器发生更改,如何关闭模式视图控制器? - How can a modal view controller be dismissed if the presenting view controller is changed? 取消iOS模态时的RN回调 - RN callback when a Modal has been dismissed for iOS 为什么简单的模态视图控制器在显示和关闭时会滞后? - Why would a simple modal view controller lag when presented and dismissed? Modal View控制器在出现内存警告时被解雇? - Modal View controller dismissed on memory warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM