networkActivityIndi​​catorVisible

时间:2010-06-13 12:19:29

标签: iphone xcode

此代码与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; 
}

张志贤

5 个答案:

答案 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),您可以在AFNetworkingActivityIndi​​catorManager中查看他们的网络活动指示器实现。

如果您选择使用此库进行网络访问,他们会自动为您处理网络活动指示器。您需要做的就是在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

  • (void)webViewDidFinishLoad:(UIWebView *)webView { [UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO; [ProgressHUD解雇]; } 并在 - (void)viewDidLoad
  • 之后使用它

[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = YES;     [ProgressHUD节目:@&#34;加载隐私政策&#34;相互作用:NO];