我知道为了在状态栏上显示/隐藏跳动,我可以使用
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;但是我的程序从许多线程发送通信请求,我需要一个位置来控制是否应该显示或隐藏脉搏。
我想到了一个集中式的类,在这个类中,每个通信请求都会注册,这个类将知道当前是否有一个或多个请求正在传输字节,并将打开跳动,否则将关闭。
这是要走的路吗?为什么当联网发生时,苹果没有自动地让脉搏出现
发布于 2010-09-02 22:50:04
试着使用类似这样的东西:
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;
}
}更新:使其线程安全
发布于 2010-09-02 22:47:08
在您的UIApplication子类singleton中包含一些逻辑似乎是处理此问题的自然方法。
//@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;
}https://stackoverflow.com/questions/3628165
复制相似问题