委托方法不在iPhone中更新UI

时间:2013-08-28 21:45:15

标签: ios delegates nsurlconnection

这很奇怪。我正在使用NSURLConnectionDelegate从Web服务中检索信息。在connectionDidFinishLoading方法中,我向委托发送消息以更新UITableView并隐藏“正在加载...”UILabel。调用委托方法并调用更新表视图和隐藏标签的代码,但标签和表视图不响应。我在IB中正确连接了所有内容,因为当我关闭/重新打开视图时,我能够隐藏/显示标签并更新表视图。任何帮助将不胜感激。

相关代码:

// File with NSURLConnection
// .h
@protocol updateTableViewDelegate <NSObject>
-(void)didFinishLoadingData;
@end

@property (nonatomic, assign) id <updateTableViewDelegate> delegate;

// .m
@synthesize delegate;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
       if (delegate && [delegate respondsToSelector:@selector(didFinishLoadingData)]) {
        [delegate didFinishLoadingData];
       }    
}

// connection started with
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];


// File with table view

// conform to <updateTableViewDelegate> in header file
// set the delegate of NSURLConnection object to self in viewDidLoad

-(void)didFinishLoadingData{

    self.viewLoading.hidden = YES;

    // Repopulate array of data for table view
    ....

    [self.tblViewLookup reloadData];
}

1 个答案:

答案 0 :(得分:0)

使用dispatch_async(dispatch_get_main_queue(),^ {//您的代码更新UI}