在做项目的时候,我遇到了这个问题。
其中一个控制器实现了keyboardWillShow& keyboardWillHide (苹果管理键盘的标准代码)。在背景点击上,UIAlertView出现(根据一些验证),UIAlertView中只有一个按钮可以简单地关闭UIAlertView。
问题出现在这里,在UIAlertView结束时,keyboardWillShow和keyboardWillHide再次调用。
下面是我遇到问题的代码,
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
{
int timeCalledShow;
int timeCalledHide;
}
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
- (IBAction)backgroundTapped:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
timeCalledShow+=1;
NSLog(@"show Time Called %d", timeCalledShow);
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
}
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)notification {
timeCalledHide+=1;
NSLog(@"Hide Time Called %d", timeCalledShow);
self.scrollView.contentInset = UIEdgeInsetsZero;
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)backgroundTapped:(id)sender {
[[[UIAlertView alloc] initWithTitle:@"Testing" message:@"Keyboard hide & show, due to alert view" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
[self.view endEditing:YES];
}
@endNotes
iOS 7.0中运行得很好。编辑我已经知道代码的工作了。但真正的问题是,a__UIAlertView如何触发a__keyboardWillShow通知?
编辑代码我已经尝试了下面的代码也是由@Chonch建议的,但是用这段代码键盘永远不会关闭。表示关闭警报后再次出现键盘。
- (IBAction)backgroundTapped:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"testing" message:@"Keyboard" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[self.textField resignFirstResponder];
}发布于 2015-08-29 14:44:49
不知道为什么会这样。这可能与UIAlertView试图使键盘状态恢复到以前的状态有关。但是请注意,使用额外的显示/隐藏调用并没有造成伤害。一般说来,你需要为多个显示调用做好准备,因为它们也是在键盘样式改变时出现的。
如果您想摆脱它们,那么使用UIAlertController代替,就像Chonch已经建议的那样,并确保键盘在警报出现之前被取消,那么它就会正常工作:
- (IBAction)backgroundTapped:(id)sender {
[self.textField resignFirstResponder];
alert = [UIAlertController alertControllerWithTitle:@"testing" message:@"Keyboard" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}请注意,使用UIAlertController时,还需要在视图控制器中保留对警报的引用,否则它将很快被释放。
发布于 2016-09-29 01:02:31
我刚解决了一个类似的问题。当警报解除后,键盘就会弹出,看上去像是苹果的窃听器。
有一个简单的解决方案:如果使用AlertController,只需将动画设置为NO即可。
[self presentViewController:alert animated:NO completion:nil];如果它能解决你的问题,请告诉我
https://stackoverflow.com/questions/32265161
复制相似问题