我有一个向服务提交表单数据的按钮。当用户点击它时,它会提交数据并提醒成功。我想在其中添加活动指示器,当用户点击按钮提交活动指示器应该移动时提交数据所需的时间,而不是在提交数据时隐藏。我很困惑我应该在哪里使用它代码,我很困惑,是否应该在回复后或其他任何地方打电话。我的代码是,
NSString *post = [NSString stringWithFormat:@"propertyfor=%@&propertytype=%@&landarea=%@&country=%@&city=%@&price=%@&description=%@&location=%@&name=%@&email=%@&phone=%@",_propertyfor,_propertytype,_landarea,_country,_city,_price,_propdes,_propdescrip,self.name.text,self.email.text,phone];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"My URL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}] resume];
答案 0 :(得分:1)
**//Start Activity Indicator here.**
NSString *post = [NSString stringWithFormat:@"propertyfor=%@&propertytype=%@&landarea=%@&country=%@&city=%@&price=%@&description=%@&location=%@&name=%@&email=%@&phone=%@",_propertyfor,_propertytype,_landarea,_country,_city,_price,_propdes,_propdescrip,self.name.text,self.email.text,phone];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"My URL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
**//Stop Activity Indicator here.**
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}] resume];
答案 1 :(得分:1)
您需要在创建会话之前开始制作动画,并停止动画制作您获得回复的动画。在您的代码中:
.
.
.
.
[activity startAnimating];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
[activity stopAnimating];
});
NSLog(@"requestReply: %@", requestReply);
}] resume];
答案 2 :(得分:0)
将followind代码替换为原始代码:
CGSize s1 = UIApplication.sharedApplication.windows[0].frame.size;
CGRect r1 = CGRectMake(s1.width/2-20, s1.height/2-20, 40, 40);
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc]initWithFrame:r1];
[self.view addSubview:act];
[act startAnimating];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
[act stopAnimating];
[act removeFromSuperview];
}] resume];