简体   繁体   中英

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...

In my viewDidload, I define a NSURLRequest to a personal websiten, and right after and before it I added a label.text=@"xxx". I know the problem doesn't come from linking the label in IB because it used to display what I wanted.

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 ? 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。

没关系,我通过将指令移至另一种方法来使其工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM