UIActivityIndi​​catorView NSURLConnection,视图未显示

发布于 2024-11-10 12:58:21 字数 2037 浏览 3 评论 0原文

当我在 NSURLConnection 请求的帮助下从服务器收集数据时,获取 UIActivityIndi​​catorView 显示时遇到问题。

我认为该请求是异步的,即在新线程中启动。我复制了 Apple 的 AdvancedTableViewCells 示例。我在 iOS 4.3 iPhone 模拟器的 XCode 中运行它。我还没有在真正的 iPhone 上测试过它。

我也用谷歌搜索了这个问题并尝试了很多建议,但感觉我忘记了一些基本的东西。下面是我在 RootViewController 类中的代码。

我相信,我只需选择一行,创建并添加 ActivityView,启动动画,然后创建 NSUrlConnection 对象,该对象开始在另一个线程中从服务器获取数据。

有什么想法吗?

@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];
    [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];
        }
    }
}

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];
    [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];
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-11-17 12:58:21

有同样的问题,尝试更改 Attributes InspectorUIActivityIndi​​catorView 的颜色 -> 样式灰色

在此处输入图像描述

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文