-(void)method1
{
[self method2];
[self method3]; //After finishing execution of method2 and its delegates I want to execute method3
}
这里,method2在调用时运行,但在执行其委托方法之前,method3开始执行。怎么避免呢?任何建议或代码,请
我在方法2
中调用了与其代理人的nsurl连接 -(void)method2
{
....
connection= [[NSURLConnection alloc] initWithRequest:req delegate:self ];
....
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
}
..
..
答案 0 :(得分:5)
使用块 - 处理起来会更容易:
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil) {
// parse your data here
[self method3];
dispatch_async(dispatch_get_main_queue(), ^{
// call method on main thread, which can be used to update UI stuffs
[self updateUIOnMainThread];
});
}
else if (error != nil) {
// show an error
}
}];
答案 1 :(得分:1)
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *)
{
[self method3]
}
答案 2 :(得分:0)
您正在使用异步网址连接。那个方法3在方法2完成之前被触发。要解决您的问题,请使用此
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self method3];
}
绝对可以。