繁体   English   中英

如何在iPhone上控制网络活动指示器

[英]How to control Network Activity Indicator on iPhone

我知道,为了在状态栏上显示/隐藏跳动者,我可以使用

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

但是我的程序从许多线程发送了comm请求,并且我需要一个位置来控制应显示还是隐藏节拍器。

我想到了一个集中化的类,在该类中,每个comm请求都将注册,并且该类将知道一个或多个请求当前是否正在传输字节,并且将打开throbber,否则将其关闭。

这是要走的路吗? 为什么在联网过程中,苹果公司没有自动使打动者出现

尝试使用如下形式:

static NSInteger __LoadingObjectsCount = 0;

@interface NSObject(LoadingObjects)

+ (void)startLoad;
+ (void)stopLoad;
+ (void)updateLoadCountWithDelta:(NSInteger)countDelta;

@end

@implementation NSObject(LoadingObjects)

+ (void)startLoad {
    [self updateLoadCountWithDelta:1];
}

+ (void)stopLoad {
    [self updateLoadCountWithDelta:-1];
}

+ (void)updateLoadCountWithDelta:(NSInteger)countDelta {
    @synchronized(self) {
        __LoadingObjectsCount += countDelta;
        __LoadingObjectsCount = (__LoadingObjectsCount < 0) ? 0 : __LoadingObjectsCount ;

        [UIApplication sharedApplication].networkActivityIndicatorVisible = __LoadingObjectsCount > 0;
    }
}

更新:使其线程安全

UIApplication子类单例中包含一些逻辑似乎是处理此问题的自然方法。

//@property bool networkThingerShouldBeThrobbingOrWhatever;
// other necessary properties, like timers
- (void)someNetworkActivityHappened {
    // set a timer or something
}
- (void)networkHasBeenQuietForABit
    // turn off the indicator.
    // UIApplcation.sharedApplication.networkActivityIndicatorVisible = NO;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM