今天写项目的时候遇到一个问题:从A界面推出B界面,B界面的textField编辑完成之后,向服务器发送编辑数据之后,成功之后弹出成功的系统提示框。代码如下:
//B界面的textfiled编辑完成点击okButton:
但这种情况在ios7 中没有出现,在ios8以上会出现:
- (void)OKButtonBeclick
{
__weak VFeedbackViewController *weakSelf = self;
[self.view endEditing:YES];
self.button.enabled = NO;
[[VFeedBackManager sharedManager] FeedBackWithcontent:self.feedbackTextView.text success:^{
[UIAlertView showWithTitle:@"Thanks for your feedback" message:nil cancelButtonTitle:nil otherButtonTitles:@[@"OK"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 0) {
[weakSelf.navigationController popViewControllerAnimated:YES];
}
}];
} failure:^(NSString *failing) {
weakSelf.button.enabled = YES;
NSLog(@"%@",failing);
}];
}
直接这样写的话会遇到一个问题:就是界面先返回,然后键盘会在A界面出现下落的状况。因为AlertView 的 block不是在主线程里面,所以会会执行先执行
[weakSelf.navigationController popViewControllerAnimated:YES]; 然后执行 [self.view endEditing:YES];
解决方案是进行延迟:等[self.view endEditing:YES]执行完毕,再执行AlertView 的 block 中得 [weakSelf.navigationController popViewControllerAnimated:YES];
延迟一段时间,等主线完成,待执行线程的函数
合理的代码是:加dispatch_after的延迟函数
- (void)OKButtonBeclick
{
__weak VFeedbackViewController *weakSelf = self;
[self.view endEditing:YES];
self.button.enabled = NO;
[[VFeedBackManager sharedManager] FeedBackWithcontent:self.feedbackTextView.text success:^{
[UIAlertView showWithTitle:@"Thanks for your feedback" message:nil cancelButtonTitle:nil otherButtonTitles:@[@"OK"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 0) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.navigationController popViewControllerAnimated:YES];
});
}
}];
} failure:^(NSString *failing) {
weakSelf.button.enabled = YES;
NSLog(@"%@",failing);
}];
}