此代码与networkActivityIndicatorVisible一起使用是否正确?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIApplication* app2 = [UIApplication sharedApplication];
app2.networkActivityIndicatorVisible = YES;
[self loadSources]; // Loads data in table view
app2.networkActivityIndicatorVisible = NO;
}
张志贤
答案 0 :(得分:17)
由于在连接仍处于活动状态时可以从多个点设置NetworkActivityIndicatorVisible
,因此您需要跟踪启用/禁用连接的呼叫数。以下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线程有时间在阻止主线程之前启动网络指示器动画。
答案 1 :(得分:5)
如果您还没有使用AFNetworking(https://github.com/AFNetworking/AFNetworking),您可以在AFNetworkingActivityIndicatorManager中查看他们的网络活动指示器实现。
如果您选择使用此库进行网络访问,他们会自动为您处理网络活动指示器。您需要做的就是在AppDelegate中进行一次调用以进行设置,他们会为您完成剩余的工作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
}
答案 2 :(得分:3)
用于跟踪多个活动的更短解决方案 - 再次使用UIApplication类别和静态变量:
@interface UIApplication (NetworkActivityIndicator)
- (void)toggleNetworkActivityIndicatorVisible:(BOOL)visible;
@end
@implementation UIApplication (NetworkActivityIndicator)
-(void)toggleNetworkActivityIndicatorVisible:(BOOL)visible {
static int activityCount = 0;
@synchronized (self) {
visible ? activityCount++ : activityCount--;
self.networkActivityIndicatorVisible = activityCount > 0;
}
}
@end
答案 3 :(得分:1)
我终于解决了它。我使用performSelectorInBackground
将加载数据执行到tableView
-(void)beginLoadSources {
[self loadSources]; // Loads data in table view
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self performSelectorInBackground:@selector(beginLoadSources) withObject:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
答案 4 :(得分:0)
在ExempleUIWebView.m中使用这些代码 之前 - (void)viewDidLoad
[UIApplication sharedApplication] .networkActivityIndicatorVisible = YES; [ProgressHUD节目:@&#34;加载隐私政策&#34;相互作用:NO];