弹出时键盘短暂出现 viewcontroller iOS
Keyboard appearing briefly when popping viewcontroller iOS
我正在试验从 UINavigationController 弹出当前 UIViewController 时数字键盘的问题。
在我当前的 UIViewController 中。我在 UINavigationBar 中有几个 UITextField 和一个 "Save" 按钮。预期行为如下:
当用户点击"Save"时,键盘必须隐藏并执行网络操作。在它的回调中,显示了一个 UIAlertView。当用户关闭此 UIAlertView 时,将引发通知并且当前 UIViewController 执行
[self.navigationController popViewControllerAnimated:YES];
问题是,如果在键盘仍然显示的情况下按下 "Save",在执行 popViewControllerAnimated 之后,键盘会短暂地从左到右出现(就好像它在之前的 UIViewController 中是可见的)。
我试过了
[myTextField resignFirstResponder]
当用户点击 "Save" 时,当用户关闭 UIAlertView 时,甚至在
viewWillDisappear
方法。其他一些答案建议使用
[self.view endEditing:YES];
但是也没用。
如果我可以使用常规键盘,那么覆盖
将是微不足道的
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
在用户点击 "Return"、"Done" 等时隐藏它。但是作为数字键盘不允许您显示 "finish" 按钮。
非常感谢任何帮助,谢谢大家的宝贵时间
试试这个:
将文本字段委托及其 return 类型设置为“完成”并将填充为数字键盘。
_textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
[_textField setReturnKeyType:UIReturnKeyDone];
现在将按钮添加到键盘:
-(void)addButtonsToKeyboard
{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil)
style:UIBarButtonItemStyleDone target:self
action:@selector(kbDoneAction:)];
UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIBarButtonItemStylePlain target:self
action:@selector(kbCancelAction:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]];
_textField.inputAccessoryView = keyboardDoneButtonView;
}
并隐藏键盘:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
您的完成操作方法是:
-(void)kbDoneAction:(id)sender
{
[_textField resignFirstResponder];
}
取消操作方法是:
-(void)kbCancelAction:(id)sender
{
[_textField resignFirstResponder];
}
我也遇到过类似的情况。我最终延迟 popViewControllerAnimated
比键盘动画持续时间(0.333
秒)长一点。
Try using the below code. It works fine for iOS 8 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:B_title
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self.navigationController popViewControllerAnimated:YES];
}];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
我正在试验从 UINavigationController 弹出当前 UIViewController 时数字键盘的问题。
在我当前的 UIViewController 中。我在 UINavigationBar 中有几个 UITextField 和一个 "Save" 按钮。预期行为如下:
当用户点击"Save"时,键盘必须隐藏并执行网络操作。在它的回调中,显示了一个 UIAlertView。当用户关闭此 UIAlertView 时,将引发通知并且当前 UIViewController 执行
[self.navigationController popViewControllerAnimated:YES];
问题是,如果在键盘仍然显示的情况下按下 "Save",在执行 popViewControllerAnimated 之后,键盘会短暂地从左到右出现(就好像它在之前的 UIViewController 中是可见的)。
我试过了
[myTextField resignFirstResponder]
当用户点击 "Save" 时,当用户关闭 UIAlertView 时,甚至在
viewWillDisappear
方法。其他一些答案建议使用
[self.view endEditing:YES];
但是也没用。
如果我可以使用常规键盘,那么覆盖
将是微不足道的- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
在用户点击 "Return"、"Done" 等时隐藏它。但是作为数字键盘不允许您显示 "finish" 按钮。
非常感谢任何帮助,谢谢大家的宝贵时间
试试这个:
将文本字段委托及其 return 类型设置为“完成”并将填充为数字键盘。
_textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
[_textField setReturnKeyType:UIReturnKeyDone];
现在将按钮添加到键盘:
-(void)addButtonsToKeyboard
{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil)
style:UIBarButtonItemStyleDone target:self
action:@selector(kbDoneAction:)];
UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIBarButtonItemStylePlain target:self
action:@selector(kbCancelAction:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]];
_textField.inputAccessoryView = keyboardDoneButtonView;
}
并隐藏键盘:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
您的完成操作方法是:
-(void)kbDoneAction:(id)sender
{
[_textField resignFirstResponder];
}
取消操作方法是:
-(void)kbCancelAction:(id)sender
{
[_textField resignFirstResponder];
}
我也遇到过类似的情况。我最终延迟 popViewControllerAnimated
比键盘动画持续时间(0.333
秒)长一点。
Try using the below code. It works fine for iOS 8 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:B_title
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self.navigationController popViewControllerAnimated:YES];
}];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}