gpt4 book ai didi

iphone - 调用connectionDidFinishLoading后更新 View Controller

转载 作者:行者123 更新时间:2023-11-29 04:06:56 26 4
gpt4 key购买 nike

我正在尝试异步加载网页内容。我不确定调用 connectionDidFinishLoading 方法后如何更新 View Controller 中的标签/其他内容。在下面的示例中,我只是尝试更新标签以显示内容已加载。我该怎么做?谢谢!

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[responseData
length]);
NSString *txt = [[NSString alloc] initWithData:responseData encoding:
NSASCIIStringEncoding];

label.text = @"DISPLAY THIS WHEN FINISHED";
}

我被告知让我的viewController成为NSURLConnectionDelegate,然后从viewDidLoad运行fetchData方法,然后使用在connectionDidFinishLoading中获取数据时为我们获取的数据。有人知道从哪里开始吗?谢谢!

最佳答案

正如 Ramy 正确指出的那样,您必须在主线程上更新 UI,然后:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %d bytes of data",[responseData length]);
NSString *txt = [[NSString alloc] initWithData:responseData
encoding: NSASCIIStringEncoding];

NSString *text = @"DISPLAY THIS WHEN FINISHED";
[label performSelectorOnMainThread:@selector(setText:) withObject:text waitUntilDone:NO]
}

关于iphone - 调用connectionDidFinishLoading后更新 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15039374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com