我的 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"];
}