ios – Swift中的活动指标

我正在尝试使用 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();
    }


}

解决方法

似乎每次调用start / stop时,都会创建一个ViewController的新实例
只需将ViewController替换为self并尝试

例如

Indicator = ActivityIndicator().StartActivityIndicator(self);

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...