0

I want to add an activity indicator to the view when a user saves information for a post. However, the swift file that controls the saving the info is not a UIView controller. It refers to a different Viewcontroller, and its settings at the top of the page are as follows:

protocol CustomCellDelegate {
    func dateWasSelected(selectedDateString: String)
    func textfieldTextWasChanged(newText: String, parentCell: CustomCell)
    func entranceFeeWasChanged(newText:String, parentCell: CustomCell)
}

This is how I have wanted to set up the activity indicator to run once the save button is tapped:

var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

and then (in a closure):

activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
            activityIndicator.center = self.view.center //centers the indicator
            activityIndicator.hidesWhenStopped = true
            activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
            view.addSubview(activityIndicator)
            activityIndicator.startAnimating()
          UIApplication.sharedApplication().beginIgnoringInteractionEvents()

But the code won't work... specifically the self.view.center and the view.addSubview gives errors. I presume this is because the swift file is not a view controller. Is there a way to get around this?

Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • Try either a) pass to your closure a reference to your view controller, b) add the activity indicator to a new `UIWindow` (above the current window). – paulvs Aug 14 '16 at 23:22
  • @paulvs how do you pass a reference from a different swift file (the view controller swift file) to the current swift file? – Runeaway3 Aug 14 '16 at 23:59
  • It depends on your code, if you're calling a closure from the view controller, add the view controller itself as an argument. If needed, add the relevant code (the closure) and I can try to help. – paulvs Aug 15 '16 at 00:08
  • @paulvs I am not calling a closure from the viewcontroller. I just want to add a 'view' (the activity indicator) to the viewcontroller from the current swift file, which is the one that begins with protocol Customcelldelegate. Is that possible? – Runeaway3 Aug 15 '16 at 00:39
  • You could use [this answer](http://stackoverflow.com/a/28932429/1305067) to get the top most view controller, and add it to its `view`. – paulvs Aug 15 '16 at 13:48

0 Answers0