[英]Objective-c : Execute a function in current class only after NSURLSession and NSXMLParser delegates complete in another class
I have a Class1 in which I have to load a UITableView
row on didSelectRowAtIndexPath
. 我有一个Class1 ,我必须在didSelectRowAtIndexPath
上加载UITableView
行。 In this didSelectRowAtIndexPath
i make a call to a method in Class2 which thereby calls NSURLSession
delegates and NSXMLParser
delegates. 在此didSelectRowAtIndexPath
我对Class2中的方法进行了调用,从而调用了NSURLSession
委托和NSXMLParser
委托。 I want to get the value of this parsed data and reload the UITableView
row in Class1 . 我想获取此已解析数据的值,然后重新加载Class1中的UITableView
行。
I want to reload UITableView
only after I get the data. 我只想在获取数据后重新加载UITableView
。 As the Delegate methods are asynchronous I am not able to get the data and so I get an empty UITableView row. 由于Delegate方法是异步的,因此我无法获取数据,因此将获得一个空的UITableView行。
What can be the solution? 有什么解决方案?
On completion of data pulling and parsing in Class2, let Class1 know: 在Class2中完成数据提取和解析后,让Class1知道:
[[NSNotificationCenter defaultCenter] postNotificationName:@"kDataIsReady" object:@{@"key":@"value"}];
You need to register Class1 as a listener early on (eg in viewDidLoad): 您需要尽早将Class1注册为侦听器(例如,在viewDidLoad中):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataReady:) name:@"kDataIsReady" object:nil];
Which then calls this method in Class1: 然后在Class1中调用此方法:
-(void)dataReady:(NSNotification *)n {
NSDictionary * d = n.object;
NSString * value = d[@"key"];
//e.g. update your data array, refresh tableView / specific cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.