gpt4 book ai didi

ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:55 25 4
gpt4 key购买 nike

在做项目的时候,我遇到了这个问题。

其中一个 Controller 实现了keyboardWillShowkeyboardWillHide(来自 Apple 的标准代码 Managing the Keyboard)。在后台点击时,UIAlertView 出现(基于一些验证),UIAlertView 中只有一个按钮可以简单地关闭 UIAlertView

问题出现在这里,在 UIAlertView 关闭时,再次调用了 keyboardWillShowkeyboardWillHide

下面是我遇到问题的代码,

#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

注意事项

  1. 我已经检查了keyboardWillShow called twice和类似的问题在这里,但找不到答案
  2. 它适用于 iOS 7.0
  3. 这是Test Code的链接

编辑我已经知道如何解决代码问题。但真正的问题是,UIAlertView 如何触发 keyboardWillShow 通知

编辑代码我试过@Chonch也建议使用下面的代码,但使用此代码键盘甚至永远不会关闭。表示关闭 Alert 后键盘再次出现。

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

Question Posted at Apple Developer Forums

最佳答案

UIAlertView 有问题,可能自从它被弃用后就再也没有出现过。
UIAlertController 替换它,你的问题就会消失

关于ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com