开发者

UIActivityIndicatorView NSURLConnection, view not showing

开发者 https://www.devze.com 2023-03-09 14:42 出处:网络
I have a problem getting a UIActivityIndicatorView to show when I collect data from a server with help from the NSURLConnection request.

I have a problem getting a UIActivityIndicatorView to show when I collect data from a server with help from the NSURLConnection request.

The request I think is asynchronous, i.e., started in a new thread. I have copied from Apple's AdvancedTableViewCells example. And I run it in XCode in the iOS 4.3 iPhone simulator. I have not tested it on a real iPhone yet.

Also I have googled this problem and tried a lot of suggestions but the feeling is that I have forgotten something basic. Below is my code from the class RootViewController.

I just select a row, create and add the activityview, startanimating, and then create the NSUrlConnection object which starts to fetch data from the server in another thread, I believe.

Any ideas?

@interface RootViewController : UITableViewController {
    NSMutableData *receivedData;
    UIActivityIndicatorView *activityView;
}

@end

...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // In my rootviewcontroller
    activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    [self.view addSubview:activityView];
   开发者_Go百科 [activityView startAnimating];
    …

    NSMutableURLRequest *tUrlRequest = [tQuery createUrlRequest:tStatId];
    NSURLConnection *tConnectionResponse = [[NSURLConnection alloc] initWithRequest: tUrlRequest delegate: self];

    if (!tConnectionResponse) {
        NSLog(@"Failed to submit request");
    } else {
        NSLog(@"Request submitted");
        receivedData = [[NSMutableData data] retain];
    }
    return;
}

...

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    NSXMLParser *tParser = [[NSXMLParser alloc] initWithData: receivedData];
    ...

    [tParser parse]; 
    ...

    [connection release];
    [receivedData release];

    [NSThread sleepForTimeInterval: 2.0]; // Just to see if activity view will show up...

    NSUInteger row = 1;  
    if (row != NSNotFound) 
    { 
        // Create the view controller and initialize it with the 
        // next level of data. 
        VivadataTViewController *vivaViewController = [[VivadataTViewController alloc] init];

        if (activityView != nil) {
            [activityView stopAnimating];
        }
    }
}


Had the same exact issue, try to change the color of the UIActivityIndicatorView under Attributes Inspector -> Style to Gray

UIActivityIndicatorView NSURLConnection, view not showing

0

精彩评论

暂无评论...
验证码 换一张
取 消