Activity Indicator doesn't work in Swift

1.5k views Asked by At

I am trying to show a simple activity indicator when my view controller loads until a segue is performed. I want to start the activity indicator on viewDidLoad but I have had no luck so far.

This is the code I was trying to use:

import UIKit

class LoadingScreen: UIViewController {
//Activity indicator view
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //start animating
        self.activityIndicatorView.startAnimating()
}
}

I get an error: EXC_BAD_INSTRUCTION which I assume means there's a problem with the code I'm using.

I then stop it after a function called delay:

delay(2.5) {

            self.activityIndicatorView.stopAnimating()
}
1

There are 1 answers

0
lchamp On

Assuming the IBOutlet is working correctly (as mentioned in your comment above). You probably want to stopAnimating your activityIndicatorView within the prepareForSegue:sender: method.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // You might not need the if statement
    if (segue.identifier == "nameOfYourSegue") {
        self.activityIndicatorView.stopAnimating()
    }
}