【问题标题】:Spin screen with connection带连接的旋转屏幕
【发布时间】:2009-08-14 01:08:09
【问题描述】:

我想知道下一步该怎么做:

我有一个应用程序可以建立一些连接来接收数据(此数据显示在屏幕上)

我想知道我可以使用哪些代码来等待每个连接,我的意思是,连接开始和完成之间的时间我想在屏幕上显示一次旋转(正在加载...)。

我已经创建了连接和旋转。我的问题是我不知道我可以使用哪个代码来管理这个以及在哪里编写这个代码。

【问题讨论】:

    标签: objective-c iphone-sdk-3.0 connection


    【解决方案1】:

    您应该考虑将 NSURLConnection 与委托一起使用。 NSURLConnection 允许您从服务器异步获取数据(它在后台运行,并在某些事件发生时通知委托)。

    然后,在您的视图控制器类中,您可以在启动连接之前立即启动微调器,并让您的委托方法之一在连接完成时停止微调器。

    【讨论】:

    • 你能给我举个例子吗?我不太清楚如何创建委托。
    • 您在答案中发布的代码看起来不错 - 现在您所要做的就是给 Connection 类一个您的视图控制器的实例,它应该 @synthesize 微调器。然后,在您调用 [theConnection start] 之前,启动您的微调器,然后在 connectionDidFinishLoading: 中再次停止它。
    • 如何创建实例?我应该在 Connection.h 中创建一个对象“视图控制器”吗??
    • 是的 - 如果您的视图控制器属于 MyViewController 类,请在 Connection.h 中添加一个类似于 MyViewController *controller; 的实例变量,然后在 Connection 的 init 方法中或在您之后设置它从控制器初始化它:connection.controller = self;
    • 谢谢,运行良好:) 我想知道(因为我的应用程序有标签栏项目)如何重新加载我单击的标签栏的视图(在加载视图消失后)谢谢
    【解决方案2】:

    这是我在课程中的代码:Connection.h,我在每个我想调用新连接以获取数据的类中创建一个对象 Connection(我不知道这是否是正确的方法)

    导入“连接.h” 导入“XMLParser.h”

    @implementation 连接 @synthesize webData、soapResults、xmlParser;

    -(Connection *) Init:(NSInteger *) methodNumber{ [超级初始化]; 方法编号 = 方法编号; 回归自我; }

    -(void)Connect:(NSString *)soapMessage{

    NSLog(soapMessage);
    
    NSURL *url = [NSURL URLWithString:@"http://.....?WSDL"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    
    //NSURLConnection *theConnection =  [NSURLConnection connectionWithRequest:theRequest delegate:self];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [theConnection start];
    
    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
    

    }

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [网络数据集长度:0]; }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [网络数据追加数据:数据]; }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"连接错误"); [连接释放]; [网络数据发布]; }

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

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSLog(theXML);
    [theXML release];
    
    
    
    
    if( xmlParser )
    {
        [xmlParser release];
    }
    
    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    
    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser:methodNum];
    
    //Set delegate
    [xmlParser setDelegate:parser];
    
    
    //[xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
    
    [connection release];
    [webData release];
    

    }

    @结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多