[英]Instructions in viewdidload not being taken care of
I am facing an annoying problem. 我面临一个烦人的问题。 I have an application who is basicly made of several methods: viewDidload, connection:didReceiveResponse, connection:didReceiveData... 我有一个基本上由几种方法组成的应用程序:viewDidload,connection:didReceiveResponse,connection:didReceiveData ...
In my viewDidload, I define a NSURLRequest to a personal websiten, and right after and before it I added a label.text=@"xxx". 在我的viewDidload中,我定义了一个NSURLRequest到一个个人网站,并在它之后添加了label.text = @“ xxx”。 I know the problem doesn't come from linking the label in IB because it used to display what I wanted. 我知道问题不在于链接IB中的标签,因为它曾经用来显示我想要的内容。
But now it seems none of those two label.text instructions are working even though I know my NSURLRequest works because the number of bytes received changes when I change the website... Why is that ? 但是现在看来这两个label.text指令都不起作用,即使我知道我的NSURLRequest也可以工作,因为当我更改网站时收到的字节数发生了变化,这是为什么? And I'm guessing the other instructions that come after aren't working either. 而且我猜想后面的其他说明也不起作用。
I will give more details when I can in case anyone can enlighten me on this. 如果有任何人可以启发我,我将提供更多详细信息。
Have a good day and thanks for your help 祝您有美好的一天,并感谢您的帮助
- (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
我会将设置逻辑移至-viewWillAppear,而不是-viewDidLoad。
没关系,我通过将指令移至另一种方法来使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.