簡體   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