在过去的几天里,我面临着一个奇怪的keyboard
问题,这个问题只发生在iPhone 5c
中。
我在Xcode-6.4
中使用objective-C
进行开发
我的环境目标是ios7
。
以下是我如何处理keyboard Notification
。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
我正在为Deregister Notification
编写这段代码。为了确保对每个文本字段使用-resignFirstResponder
。
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self hideKeyBoard];
[self.view endEditing:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)hideKeyBoard{
[kAgeTextField resignFirstResponder];
[kSchoolTextField resignFirstResponder];
}
在提交按钮中,我检查了一些条件,然后我显示了一个AlertView
。
- (IBAction)submitClicked:(id)sender
{
if(validated)
{
[self.view endEditing:YES];
[self hideKeyBoard];
[self.view resignFirstResponder];
[self makeApiCall];
}
}
现在,当我从服务器获得成功/失败响应时,我正在这样做。这是从服务器获得响应后运行的块:
-(void)SuccessfulWithServerInfo:(id)responseInfo
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
dispatch_async(dispatch_get_main_queue(),^{
[appDelegate hideProgressViewFromView:self.view];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Thanks for coming" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:YES];
});
}
问题当我得到alertBox并按下ok时。然后键盘自动打开并关闭。这只发生在iPhone 5C上。我在4秒、5秒、6秒和6秒以上检查过。一切都很好。
如果有人知道,请提供帮助。
您在执行popToRootViewController的同时显示警报。可能这会引起问题。
- 显示警报
- 句柄警报视图方法
-
在警报视图的方法中写入[self.navigationController popToRootViewControllerAnimated:YES]。
[UIAlertView showWithTitle:@"" message:@"Thanks for coming" cancelButtonTitle:@"OK" otherButtonTitles:nil] alertViewStyle:UIAlertViewStyleDefault tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex, NSString *text) { if(buttonIndex == 1) { [self.navigationController popToRootViewControllerAnimated:YES]; } }];
希望这对你有帮助。
经过一些研究,我在stackOverflow中找到了这个答案。
它们是ios7和ios8中AlertView行为的一些变化。
我用这个代码来解决我的问题:
[self performSelector:@selector(showAlertView) withObject:nil afterDelay:0.6];
有关详细答案,请参阅本SO答案