网络活动指示器未出现在iOS应用中

时间:2015-05-16 00:04:02

标签: ios activity-indicator

在我的iOS应用中,我想在顶部状态栏中显示网络活动指示器。

我添加了以下内容:

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

但活动指标永远不会出现。

有谁知道可能出错了什么?

以下是完整代码:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    // load sets
    [self loadSets];
}

-(void)loadSets{

    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"in loadSets");

        // show loading animation
        UIView *loadingView = loadingIndicator;
        loadingView.center = CGPointMake(screenWidth/2, screenHeight/2);
        [self.view addSubview:loadingView];
        [loadingIndicator startAnimating];

        self.userSets = [[NSMutableArray alloc]init]; // re-initialize userSets

        dispatch_async(bgQueue, ^{
            NSString *userURLString = [userBaseUrl stringByAppendingFormat:@"/%@.json?auth_token=%@", username, auth_token];
            NSLog(@"userURLString %@", userURLString);
            NSURL *userURL = [NSURL URLWithString:userURLString];
            NSData * userData = [NSData dataWithContentsOfURL:userURL];

            dispatch_async(dispatch_get_main_queue(), ^{
                if(userData){
                    [self fetchSets:userData];
                    // remove loading animation
                    [loadingView removeFromSuperview];
                }else{
                    // error with authentication - should log out and require relogin
                    //                [self logoutClick];
                }
            });
        });

    });
}

-(void)fetchSets:(NSData *)responseData{
    NSError * error;
    NSDictionary * json = [NSJSONSerialization
                           JSONObjectWithData:responseData
                           options:kNilOptions
                           error:&error];
    if(json){
        NSArray *sets = [json objectForKey:@"sets"];
        for (NSDictionary *currentSet in sets){
            Set *userSet = [[Set alloc] init];
            userSet.name = [currentSet objectForKey:@"name"];
            userSet.videoURL = [[currentSet objectForKey:@"media"] objectForKey:@"mp4"];
            userSet.gifURL = [[currentSet objectForKey:@"media"] objectForKey:@"gif"];
            userSet.imgURL = [[currentSet objectForKeyedSubscript:@"media"] objectForKey:@"img"];
            userSet.setID = [currentSet objectForKey:@"id"];
            [self.userSets addObject: userSet];

        }
        NSLog(@"trying to reload table data with userSets length %d", [self.userSets count]);

        [self.collectionView reloadData];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"done loading table data");
        });
    }
}

0 个答案:

没有答案