我的NavigationController中的RootViewController有一个UIButton。单击此按钮时,某些数据将从服务器加载,最后会出现一个新视图。这可能需要几秒钟,因此我想在此期间显示MBProgressHUD以向用户显示正在发生的事情。但它不起作用。
加载数据并显示HUD,所有这些都发生在我的prepareForSegue-Method中,它看起来像这样:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"showAllPoints"])
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
HUD.labelText = @"Loading data";
HUD.detailsLabelText = @"Please wait...";
HUD.mode = MBProgressHUDModeAnnularDeterminate;
[self.navigationController.view addSubview:HUD];
[HUD showWhileExecuting:@selector(prepareJSONData:)
onTarget:self withObject:
[self getJSONData] animated:YES];
[self prepareJSONData:[self getJSONData]];
[segue.destinationViewController setPoints:pointsArray];
}
}
没有任何事情发生,所以我们错了吗?有什么问题?
编辑: 以下是方法getJSON和prepareJSONData:
-(NSData *)getJSONData
{
NSURL *url = [NSURL URLWithString:@"http://www.example.de/example.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
return urlData;
}
-(void)prepareJSONData:(NSData *)jsonData
{
NSError *error;
NSDictionary *wholeDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
allWutpunkteArray = [wholeDict objectForKey:@"points"];
}
答案 0 :(得分:-1)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"showAllPoints"])
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
HUD.labelText = @"Loading data";
HUD.detailsLabelText = @"Please wait...";
HUD.mode = MBProgressHUDModeAnnularDeterminate;
[self.navigationController.view addSubview:HUD];
[HUD showWhileExecuting:@selector(prepareJSONData:[self getJSONData])
onTarget:self withObject:
nil animated:YES];
[segue.destinationViewController setPoints:pointsArray];
}
}
希望这会有所帮助......