[英]iphone :- what is difference between activity indicator and network activity indicator?
[英]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.