我正在尝试使用
swift制作活动指示器,但有一些我缺少的东西
以下是启动活动按钮按钮的代码:
Indicator = ActivityIndicator().StartActivityIndicator(ViewController());
这是停止活动按钮的代码
ActivityIndicator().StopActivityIndicator(ViewController(),indicator: Indicator);
和ACtivity指标类的代码是
class ActivityIndicator: NSObject { var myActivityIndicator:UIActivityIndicatorView! func StartActivityIndicator(obj:UIViewController) -> UIActivityIndicatorView { self.myActivityIndicator = UIActivityIndicatorView(frame:CGRectMake(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 Problem Solved
while am writing code I kNow that time that I have to pass self in the
function but at that time I didn’t kNow What type is self referencing
so I I can’t receive until I kNow the type then I decide to try to
receive different type (UIView,
UIActivityIndicatorView,UIViewController) then I found That it should
be UIViewController but I forgot that it should be pass with self and
I looking for what to pass in function calling and I thought let’s try
UIViewController and it didn’t give me error so I thought it’s right
but when it’s not working I thought I am close enough so it’s better
to ask expert about the problem. And as I expected problem is solved
within few minutes
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(); } }