0
votes

I am trying to display an activity indicator when the user hits the login button. If I put the startActivityIndicator() code in viewDidLoad() it shows on the screen exactly as expected. When I execute it as the first step in btnSignIn() it never appears. A little lost, so i'm hoping the Stack guru's can help...

// Here are the variable declarations
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
var loadingView: UIView = UIView()
var viewCenter:CGPoint!

@IBAction func btnSignIn(sender: AnyObject) {

    startActivityIndicator()

    if validateEmailAddress(txtEmailAddress.text!) == false {
        stopActivityIndicator(self.loadingView)
        return
    }

    if validatePassword(txtPassword.text!) == false {
        stopActivityIndicator(self.loadingView)
        return
    }

    PFUser.logInWithUsernameInBackground(txtEmailAddress.text!, password:txtPassword.text!) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {

            // Successful login.
            self.txtPassword.resignFirstResponder()
            self.txtEmailAddress.resignFirstResponder()

            self.getUserInfo()

        } else {
            self.stopActivityIndicator(self.loadingView)
            // The login failed. Display alert.
            self.displayAlert("Whoops!", message: "Email or Password are incorrect.")
        }
    }
}

func startActivityIndicator() {

    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = viewCenter
    print(viewCenter)
    loadingView.backgroundColor = UIColorFromRGB("444444", alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);

    view.addSubview(loadingView)
    loadingView.addSubview(activityIndicator)

    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

    activityIndicator.startAnimating()

}

func stopActivityIndicator(uiView: UIView) {

    activityIndicator.stopAnimating()
    loadingView.removeFromSuperview()
    UIApplication.sharedApplication().endIgnoringInteractionEvents()
}
1
@AnthonyDito UIActivityIndicatorView has got no method startActivityIndicator()oyvindhauge
@AnthonyDito I figured that's what you meant. He is already calling that method though.oyvindhauge
And it's weird...calling the method exactly like i'm doing here from viewDidLoad() works without issue.Robert
Are you sure you're not hitting one of the two conditionals before the call to Parse? Did you test with breakpoints?oyvindhauge
Yeah, I set breakpoints to make sure it doesn't him them, as well as a breakpoint to make sure the activity indicator code is executing.Robert

1 Answers

1
votes

Using my delay utility (see here: https://stackoverflow.com/a/24318861/341994), rewrite like this:

@IBAction func btnSignIn(sender: AnyObject) {
    startActivityIndicator()
    delay(0.1) {
        if validateEmailAddress(txtEmailAddress.text!) == false {
        // ... everything else goes here ...
    }
}

The delay gives the activity indicator a chance to appear and start spinning.