Activity indicator in Swift

I am trying to make an activity indicator using swift, but there is something that I do not see

Here is the code for the start activity button:

Indicator = ActivityIndicator().StartActivityIndicator(ViewController()); 

And here is the code for the activity stop button

  ActivityIndicator().StopActivityIndicator(ViewController(),indicator: Indicator); 

and code for the ACtivity indicator class

 class ActivityIndicator: NSObject { var myActivityIndicator:UIActivityIndicatorView! func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView { self.myActivityIndicator = UIActivityIndicatorView(frame:CGRectMake(100, 100, 100, 100)) as UIActivityIndicatorView; self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray self.myActivityIndicator.center = obj.view.center; obj.view.addSubview(myActivityIndicator); self.myActivityIndicator.startAnimating(); return self.myActivityIndicator; } func StopActivityIndicator(obj:UIViewController,indicator:UIActivityIndicatorView)-> Void { indicator.removeFromSuperview(); } } 

Thanks to solving the problem

While I am writing the code, I know that the time I have to pass to myself at that time but I did not know which type is an independent reference so I cannot get it until I know the type, then I decided to try to get a different type (UIView , UIActivityIndicatorView, UIViewController), then I found that it should be a UIViewController, but I forgot that it should go through itself and I'm looking for what to pass to the call functions, and I thought let it try the UIViewController, and it did not give me an error so I thought it was right, but when it doesn’t work, I thought I was close enough to he better ask an expert about the problem. And since I expected the problem to be resolved in a few minutes

version of Swift 3

 class ActivityIndicator: NSObject { var myActivityIndicator:UIActivityIndicatorView! func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView { self.myActivityIndicator = UIActivityIndicatorView(frame:CGRect(x: 100, y: 100, width: 100, height: 100)) as UIActivityIndicatorView self.myActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray self.myActivityIndicator.center = obj.view.center; obj.view.addSubview(myActivityIndicator); self.myActivityIndicator.startAnimating(); return self.myActivityIndicator; } func StopActivityIndicator(obj:UIViewController,indicator:UIActivityIndicatorView)-> Void { indicator.removeFromSuperview(); } } 
+5
source share
1 answer

It seems that every time you call start / stop, you create a new instance of ViewController Just replace ViewController with self and try

for instance

 Indicator = ActivityIndicator().StartActivityIndicator(self); 
+5
source

All Articles