IOS中单击按钮的活动指示器



我有一个向服务提交表单数据的按钮。当用户单击它时,它会提交数据并发出成功警报。我想在其中添加活动指示器,当用户单击提交按钮提交数据所需的时间时,活动指示器应该移动,而不是在提交数据时隐藏。我很困惑我应该在代码中的什么地方使用它,我对它是在响应后还是在其他任何地方调用感到困惑。我的代码是,

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];
**//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];

您需要在创建会话之前开始动画,并停止对收到回复的会话进行动画处理。在您的代码中:

.
.
.
.
[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];

将以下代码替换为原始代码:

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];

最新更新