【发布时间】:2015-03-22 10:57:22
【问题描述】:
我有一些从服务器获取的数据,然后显示在我的 UIViewController 类中。为此,我有两个课程。 UIViewController 和另一个名为 ServerCommunicator 的。 UIViewController 是 ServerCommunicator 类的委托。 serverCommunicator 如下所示:
- (void)fetchServerData:(NSString *) serverAddress{
NSURL *url = [[NSURL alloc] initWithString:serverAddress];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
[self.delegate fetchingSongsFailedWithError:error];
} else {
[self.delegate receivedSongsJSON:data];
}
}];
}
UIViewController 分配 serverCommunicator,将自己设置为委托,然后发出获取请求。
- (void)viewDidLoad {
[super viewDidLoad];
self.songServerCommunicator = [[serverCommunicator alloc] init];
self.songServerCommunicator.delegate = self;
[self.songServerCommunicator fetchServerData:<some_server_ip>];
}
完成后,它实现了所需的协议方法:
- (void)receivedSongsJSON:(NSData *)data{
NSLog(@"received server response");
/* Parses the data and displays in textfield/imageview */
}
我的问题是,当我显示在委托方法中接收到的数据时,它不会立即反映在 UI 中。这很奇怪,有时它会在 20 秒后自行显示,有时需要一分钟。我不确定发生了什么事。我知道数据是立即获取的,因为在 UIView 更新之前记录的消息会被打印出来。
感谢您对此的任何帮助。
【问题讨论】:
-
编辑您的代码以添加您在 UI 中设置值的方式
-
请告诉我们您设置这些值的代码
-
谢谢大家。问题就像 JOJO,abdullah an rob 在下面指出的那样。
标签: ios objective-c xcode uiview uiviewcontroller