【发布时间】:2015-08-28 07:03:38
【问题描述】:
在做项目时,我遇到了这个问题。
其中一个控制器实现keyboardWillShow 和keyboardWillHide(来自Apple 的标准代码Managing the Keyboard)。
在后台点击时,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];
}
@end
备注
- 我已经在这里查看keyboardWillShow called twice 和类似问题,但找不到答案
- 与
iOS 7.0配合使用效果很好 - 这里是Test Code的链接
编辑
我已经知道代码的工作了。但真正的问题是,UIAlertView 如何触发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];
}
【问题讨论】:
-
UIAlertView 从 iOS8.0 开始被弃用:developer.apple.com/library/prerelease/ios/documentation/UIKit/…
-
@AbdAl-rhmanTaherBadary 我已经检查过了,但它没有提供任何解决方案。我需要知道警报视图行为背后的原因。
-
@pawan 它提供了一个解决方案,您可以在显示警报视图之前取消订阅键盘通知,并在您关闭警报视图后再次订阅,无论如何这里是另一个stackoverflow.com/questions/30498972/…
-
好吧,因为它已被弃用,任何事情都可能发生,但这肯定是一个错误,但这是一个很好的观察,尽管stackoverflow.com/questions/30498972/…
标签: ios objective-c xcode uialertview ios8.3