Search code examples
swiftactivity-indicator

My activity indicator is not appearing


I want to show an activity indicator after the user enters login credentials and clicks the login button. This seemed straight forward but the indicator is not showing when I run (build succeeded). I am not sure if this is because when the app runs for real, it will be passing the credentials up to a server for verification but that code is not active yet, so the login is instantaneous, or if I have written this wrong but not to cause an error. Thank you for your help.

 @IBAction func loginButtonTapped(sender: AnyObject) {

    //add error checking for loginEntered and passwordEntered if they are blank
    if loginEntered.text == "" || passwordEntered.text == "" {

        //Define the variable error for the message that should be displayed
        self.alertError = "Please enter your Login ID and/or Password"

    }

    if self.alertError != ""  { //if there is an error with the login

        //Display the error message on screen
        self.displayAlert("Missing Required Information", error: alertError)

        //reset error variable
        self.alertError = ""

        //return the user to the LoginID field and empty it if there is data in it. Empty the password field too.  ?? How do I do that??

    } else { //the login ID and password are entered

        //setup for a spinner to notify the user the app is working on something
        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 80, 80))
        activityIndicator.center = self.view.center;
        activityIndicator.backgroundColor = (UIColor(white: 0.3, alpha: 0.5)) //creates a background behind the spinner
        activityIndicator.layer.cornerRadius = 10
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()

The code goes on to several if statements based on the login credentials and returning data from the server. Finally, is stops ignoring interaction events. Finally, I put this same code into an IBAction for a button I created to test the code. It works for the button.


Solution

  • Your code is fine or rather I would say it works, as mention is because the process takes milliseconds and not persives, I recommend that you put breakpoints in your code so that when you press Build can view the interaction to steps in your code.