Search code examples
swiftswift3uistoryboardsegueuiactivityindicatorview

How to show activity indicator while passing between view controller


Lets say I have 2 ViewControllers, in my MainViewController I have a button which performs a segue to SecondViewController. When button tapped, I'm saving some initial data to coreData, so it takes some time.

Here is the thing that I want to do; While passing between ViewControllers, I want to show ActivityIndicator, but its starts after SecondViewController is opened. Could you help me? I'm new to Swift.

Here is the code I used in my MainVC:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if (segue.identifier == "SecondViewController") {

            SwiftSpinner.show("Loading") // Act. indicator found on github

            willRunOnce() // Here Im saving data to CoreData

            SwiftSpinner.hide()
        }

    }

Solution

  • Instead of adding code of ActivityIndicator in prepare(for:sender:) method you need to call it in the Button action and after that call performSegue(withIdentifier:sender:) method.

    @IBAction func onBtnSkip(_ sender: UIButton) {
    
        SwiftSpinner.show("Loading") // Act. indicator found on github
        willRunOnce() // Here Im saving data to CoreData
        SwiftSpinner.hide()
    
        //Now performSegue 
        self.performSegue(withIdentifier: "identifier", sender: nil)
    }