Author Topic: How to Display an Activity Indicator in a Scene with a Table View  (Read 6492 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
If you are fetching data from the web to fill a table view and want to display an activity indicator in the center of the view while waiting for data, you can add the activity indicator to the AppDelegate window, which makes it handy to reuse from multiple scenes.

You can instantiate and set up the activity indicator like this:

Code: [Select]
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        // Set the activity indicator to the center of the main window
        self.activityIndicator.center = self.window!.center
        self.activityIndicator.hidesWhenStopped = true
        self.window?.addSubview(self.activityIndicator)
        return true
    }

To use the activity indicator in a particular scene, bring it to front first:

Code: [Select]
appDelegate.window?.bringSubviewToFront(appDelegate.activityIndicator)
            appDelegate.activityIndicator.startAnimating()

All the best!
Kevin McNeish
Author of Learn to Code in Swift: https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
Check out our Robot Tic-Tac-Toe app: https://itunes.apple.com/us/app/robot-tic-tac-toe/id990638517?mt=8
« Last Edit: November 15, 2015, 10:10:32 AM by kjmcneish »