我正在使用 AFNetworking,我的成功 block 工作正常,但我的 View 总是首先加载。我没有为我的 View 使用 TableView ,只有标签和需要刷新的图像。我现在该怎么办?
-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
NSLog(@"%@",IDs);
NSDictionary *params = @{@"Id" : IDs};
NSLog(@"%@",link);
[manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
clubDC.cDName = [responseObject objectForKey:@"name"];
clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
clubDC.cDImage = [responseObject objectForKey:@"image"];
clubDC.cDLong = [responseObject objectForKey:@"long"];
clubDC.cDLat = [responseObject objectForKey:@"lat"];
clubDC.cDAddress = [responseObject objectForKey:@"address"];
clubDC.cDSummary = [responseObject objectForKey:@"summary"];
clubDC.cDStat = [responseObject objectForKey:@"stat"];
clubDC.cDUS = [responseObject objectForKey:@"us"];
clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];
[dataArray addObject:clubDC];
success(dataArray);
if (dataArray.count == 0)
{
ALERT_VIEW(@"Please check your internet connection.");
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
ALERT_VIEW(@"Error while loading data.")
}];
}
我的 venuVC.m 是这样的,我想在其中显示数据
-(void)reloadView
{
clubDetail = [[ClubDetailDC alloc] init];
venu_dataArray = [[NSMutableArray alloc] init];
upcomingArray = [[NSMutableArray alloc] init];
GET_DBHANDLER
// venu_dataArray = [dbHandler get_club_detail:venuID];
NSString *strVenuID = [NSString stringWithFormat:@"%d",venuID];
NSLog(@"%@",strVenuID);
[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
{
venu_dataArray = array;
// NSLog(@"%d",array.count);
}];
}
-(void)viewWillAppear:(BOOL)animated
{
[self reloadView];
for (clubDetail in venu_dataArray)
{
lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
[imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
}
UIFont* font_name = [UIFont fontWithName:@"Corbel" size:17];
UIFont* bold_font = [UIFont fontWithName:@"Corbel-Bold" size:17];
lbl_upcomingEvents.font = font_name;
lbl_Unighted.font = bold_font;
lblDescription.font = [UIFont fontWithName:@"Corbel" size:11];
lblCheckIn.font = [UIFont fontWithName:@"Corbel" size:13];
lblCount.font = [UIFont fontWithName:@"Corbel" size:12];
lblGoodEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
lblHotEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
}
我的问题是首先加载 View ,然后从成功 block 获取数据。谁能帮忙?
谢谢。
最佳答案
你可以通过两种方式做到这一点,要么在你的 View 中编写函数,代码就像这样
-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
NSLog(@"%@",IDs);
NSDictionary *params = @{@"Id" : IDs};
NSLog(@"%@",link);
[manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
clubDC.cDName = [responseObject objectForKey:@"name"];
clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
clubDC.cDImage = [responseObject objectForKey:@"image"];
clubDC.cDLong = [responseObject objectForKey:@"long"];
clubDC.cDLat = [responseObject objectForKey:@"lat"];
clubDC.cDAddress = [responseObject objectForKey:@"address"];
clubDC.cDSummary = [responseObject objectForKey:@"summary"];
clubDC.cDStat = [responseObject objectForKey:@"stat"];
clubDC.cDUS = [responseObject objectForKey:@"us"];
clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];
for (clubDetail in venu_dataArray)
{
lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
[imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
}
[dataArray addObject:clubDC];
success(dataArray);
if (dataArray.count == 0)
{
ALERT_VIEW(@"Please check your internet connection.");
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
ALERT_VIEW(@"Error while loading data.")
}];
}
或者如果你想在单独的类中使用你的函数,那么在你的成功 block 中这样做
[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
{
venu_dataArray = array;
// NSLog(@"%d",array.count);
for (clubDetail in venu_dataArray)
{
lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
[imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
}
}];
关于ios - View 未填充数据 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22318688/