2017-07-17 28 views
1

ログイン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) 
       } 
      } 
     } 
    } 

ので、私は最初のステップは、おそらくストーリーボードでVCへの活動の指標をドラッグしているが、何が次ですか?知っていますか

答えて

2
 In your storyboard, you can find checkbox. 
  1. startsAnimating
  2. HidesWhenStopsは、(ストーリーボードでこれを確認してください。)

    @IBOutlet weak var activityIndicator: UIActivityIndicator! 
    
    @IBAction func loginAction(_ sender: AnyObject) { 
    activityIndicator.startAnimating() 
    
    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) 
        activityIndicator.stopAnimating() 
        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") 
          activityIndicator.stopAnimating() 
          //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) 
         } 
        } 
    } 
    

    }

5

作成する必要があります。ドラッグされたUIActivityIndi​​catorのIBOutletを作成する必要があります。次に、viewDidLoad funcこのUIActivityIndi​​catorをIBOutletで非表示にします。ログインボタンをクリックすると、このactivityIndi​​catorの表示と非表示を切り替えることができます。

3

ストーリーボードからあなたのViewControllerにあなたの活動の指標のIBOUtletを作成する - Creating Outlet

あなたは、あなたののviewDidLoadやストーリーボードに

activityIndicator.hidesWhenStopped = true; 

以下のプロパティを設定することができますし、それを開始したいとき、

activityIndicator.startAnimating(); 

を呼び出し、アニメーションからそれを停止する -

activityIndicator.stopAnimating(); 
2

UITextFieldのIBOutletsを作成したのと同じ方法で、UIActivityIndicatorで1つ作成します。ストーリーボードでインジケータのhidesWhenStoppedがtrueに設定されていることを確認してください。

そして、今スタートを行うのviewDidLoad

var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 

    // Add below code in viewDidLoad 

self.activityIndicator.hidesWhenStopped = true 
self.activityIndicator.center = view.center 
self.view.addSubView(self.activityIndicator) 

でそれをカスタマイズし、あなたのサインインメソッドを呼び出す前に、それをアニメーション化し、あなたがプログラムで、あなたのクラスで&をUIActivityIndi​​catorViewを作成することができ、完了ハンドラ

@IBOutlet weak var activityIndicator: UIActivityIndicator! 
//... 
activityIndicator.startAnimating() 
Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in { 
activityIndicator.stopAnimating() 
//... 
} 
3

にそれを止めます&必要なときにアニメーションを停止する

//Login Action 
    @IBAction func loginAction(_ sender: AnyObject) { 
     self.activityIndicator.startAnimating() 
     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 
       self.activityIndicator.stopAnimating() 
       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) 
       } 
      } 
     } 
1

代替アプローチ。 UIActivityViewControllerプログラムで追加:LoginViewControllerクラスにおいて

  1. 追加する

    myActivityIndi​​cator = UIActivityIndi​​catorView(activityIndi​​catorStyle:UIActivityIndi​​catorViewStyle.gray)させviewDidLoad()

  2. myActivityIndi​​cator.hidesWhenStoppedを追加します= true myActivityIndi​​cator.center = view.center ビュー。それ以外でaddSubview(myActivityIndi​​cator)@IBAction func loginAction(_ sender: AnyObject)

  3. 一部 は

    activityIndi​​cator.startAnimating() Auth.auth()SIGNIN(withEmail追加します。self.emailTextField.text!パスワード:self.passwordTextFieldを。テキスト!){(ユーザー、エラー){ activityIndi​​cator.stopAnimating()内は

1

私はきちんと進行HUDを使用するクラスを書かれています。あなただけで

ERProgressHud.hide() 

... HUDは書き込みの進行状況を隠すため

ERProgressHud.show() 

... HUDは書き込みの進行状況を示すために、プロジェクト... https://github.com/emraz/ERProgressHud

にクラスをドラッグアンドドロップする必要がありますあなたのコード..

//ログインアクション

@IBActionのFUNCのLoginActionの(_差出人: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) 
    return 

} 
else { 

    ERProgressHud.show() 

    Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in 

     ERProgressHud.hide() 
     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) 
     } 
    } 
} 
} 
関連する問題