viewdidload中的说明未被处理

时间:2011-08-23 15:19:48

标签: objective-c ios nsurlrequest

我正面临一个恼人的问题。我的应用程序基本上由几种方法组成: viewDidload,connection:didReceiveResponse,connection:didReceiveData ...

在我的viewDidload中,我将NSURLRequest定义为个人网站,在它之后和之前我添加了label.text = @“xxx”。我知道问题不是来自IB中的标签链接,因为它曾用于显示我想要的内容。

但是现在似乎这两个label.text指令都没有工作,即使我知道我的NSURLRequest有效,因为当我更改网站时收到的字节数发生了变化......为什么会这样?我猜测之后的其他指令也不起作用。

我会尽可能提供更多细节,万一有人可以就此启发我。

祝你有个美好的一天,谢谢你的帮助

- (void)viewDidLoad {
 [super viewDidLoad];
 label.text=@"rrr";

request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mywebsite.aspx?example=5"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
label.text=@"aeza";

 NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection) {
    receiveddata=[[NSMutableData data] retain];
    label.text=@"NO BUG";
}
else {
label.text=@"BUG";
}
datastring = [[NSString alloc] initWithData:receiveddata encoding:NSUTF8StringEncoding];

components=[datastring componentsSeparatedByString:@"|"];

label.text=datastring;
[datastring release];
}

-(void) connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse       *)response
{
[receiveddata setLength:0];
}

-(void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
{
[receiveddata appendData:data];
}

-(void)connection: (NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[receiveddata release];
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receiveddata length]);
[connection release];
[receiveddata release];
}
@end

2 个答案:

答案 0 :(得分:1)

我会将该设置逻辑移动到-viewWillAppear,而不是-viewDidLoad。

答案 1 :(得分:0)

没关系,我通过将指令移动到另一种方法来实现这一点。