单击"登录"后添加活动指示器?

And*_*Qin 1 uiactivityindicatorview ios swift

我想为我的Login VC添加一个活动指示器,以便用户在点击"登录"按钮后会看到该微调器.我做了多次尝试但都失败了.即使我输入了隐藏活动指示器的代码,它也只是在点击"登录"按钮之前保持动画.我删除了这些代码,并在下面有我的原始代码(没有活动指示符).

import UIKit
import Firebase

class LoginViewController: UIViewController {
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView = UIImageView(frame: view.bounds)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = #imageLiteral(resourceName: "background")
        imageView.center = view.center
        view.addSubview(imageView)
        self.view.sendSubview(toBack: imageView)
    }

    //Outlets
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    //Login Action
    @IBAction func loginAction(_ sender: AnyObject) {
        if self.emailTextField.text == "" || self.passwordTextField.text == "" {
            //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

            let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.present(alertController, animated: true, completion: nil)
        } else {
            Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in
                if error == nil {
                    //Print into the console if successfully logged in
                    print("You have successfully logged in")

                    //Go to the HomeViewController if the login is sucessful
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                    self.present(vc!, animated: true, completion: nil)
                } else {
                    //Tells the user that there is an error and then gets firebase to tell them the error
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

                    self.present(alertController, animated: true, completion: nil)
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以我知道第一步可能是将活动指示器拖到故事板中的VC,但下一步是什么?

Sur*_*eet 5

您需要创建一个拖动的UIActivityIndi​​catorIBOutlet.然后在viewDidLoadfunc中隐藏这个UIActivityIndi​​cator和它的IBOutlet.当您单击"登录"按钮时,取消隐藏此activityIndi​​cator并再次隐藏,一旦收到登录响应.