iphone – networkActivityIndi​​catorVisible

iphone – networkActivityIndi​​catorVisible,第1张

概述此代码与networkActivityIndi​​catorVisible一起使用是否正确? - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; UIApplication* app2 = [UIApplication sharedApplication]; app2.net 此代码与networkActivityIndi​​catorVisible一起使用是否正确?

- (voID)vIEwWillAppear:(BOol)animated {    [super vIEwWillAppear:animated];    UIApplication* app2 = [UIApplication sharedApplication];     app2.networkActivityIndicatorVisible = YES;     [self loadSources];  // Loads data in table vIEw    app2.networkActivityIndicatorVisible = NO; }

张志贤

解决方法 由于NetworkActivityIndi​​catorVisible可以在连接仍处于活动状态时从多个点设置,因此您需要跟踪启用/禁用它的调用数.以下UIApplication类别仅使用静态变量:

// file UIApplication+NetworkActivity.h@interface UIApplication (NetworkActivity)- (voID)showNetworkActivityIndicator;- (voID)hIDeNetworkActivityIndicator;@end// file UIApplication+NetworkActivity.m#import "UIApplication+NetworkActivity.h"static NSInteger activityCount = 0;@implementation UIApplication (NetworkActivity)- (voID)showNetworkActivityIndicator {    if ([[UIApplication sharedApplication] isstatusbarHIDden]) return;    @synchronized ([UIApplication sharedApplication]) {        if (activityCount == 0) {            [self setNetworkActivityIndicatorVisible:YES];        }        activityCount++;    }}- (voID)hIDeNetworkActivityIndicator {    if ([[UIApplication sharedApplication] isstatusbarHIDden]) return;    @synchronized ([UIApplication sharedApplication]) {        activityCount--;        if (activityCount <= 0) {            [self setNetworkActivityIndicatorVisible:NO];            activityCount=0;        }        }}@end

现在在您的客户端代码中导入UIApplication NetworkActivity.h并调用

// on connection started:[[UIApplication sharedApplication] showNetworkActivityIndicator];// on connection finished:[[UIApplication sharedApplication] hIDeNetworkActivityIndicator];

如果您担心指示灯只闪烁一秒钟,则不需要后台处理.只需调用[self performSelector:@selector(loadSources)withObject:Nil afterDelay:0.1],这样UI线程就有时间在阻塞主线程之前启动网络指示器动画.

总结

以上是内存溢出为你收集整理的iphone – networkActivityIndi​​catorVisible全部内容,希望文章能够帮你解决iphone – networkActivityIndi​​catorVisible所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1037642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存