首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何控制iPhone上的网络活动指示器

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

Stack Overflow用户
提问于 2010-09-02 22:36:06
回答 2查看 3K关注 0票数 4

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

代码语言:javascript
复制
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

但是我的程序从许多线程发送通信请求,我需要一个位置来控制是否应该显示或隐藏脉搏。

我想到了一个集中式的类,在这个类中,每个通信请求都会注册,这个类将知道当前是否有一个或多个请求正在传输字节,并将打开跳动,否则将关闭。

这是要走的路吗?为什么当联网发生时,苹果没有自动地让脉搏出现

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-02 22:50:04

试着使用类似这样的东西:

代码语言:javascript
复制
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;
    }
}

更新:使其线程安全

票数 7
EN

Stack Overflow用户

发布于 2010-09-02 22:47:08

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

代码语言:javascript
复制
//@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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3628165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档