gpt4 book ai didi

ios - TextField becomeFirstResponder 不显示键盘

转载 作者:可可西里 更新时间:2023-11-01 05:08:37 27 4
gpt4 key购买 nike

在我的程序中,我需要显示来自文本字段的文本,它在工具栏中不是用户交互的,它是键盘的附属 View 。当我按下按钮时,这部分工作。然后,如果设备旋转,我需要隐藏键盘。这部分也可以正常工作。但是当我回到 portret 并按下需要显示键盘的按钮时,什么也没有。

这是我的代码:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController{
UITextField *invisibleTextfield;
UITextField *fieldToEnterText;
}
- (IBAction)buttonAction:(id)sender {
[invisibleTextfield becomeFirstResponder];
}

#pragma mark - Text Field
- (UIToolbar *)keyboardToolBar {

UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setOpaque:NO];
[toolbar setTranslucent:YES];
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:fieldToEnterText];
UIBarButtonItem *fixItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];


NSArray *itemsArray = @[fixItem,textFieldItem,fixItem];

[toolbar setItems:itemsArray];

return toolbar;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
if ([invisibleTextfield isFirstResponder]) {
NSLog(@"did began editing invisible");
[fieldToEnterText becomeFirstResponder];
fieldToEnterText.text = _textField.text;
}
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
_textField.text = fieldToEnterText.text;
}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([fieldToEnterText isFirstResponder]) {
[fieldToEnterText resignFirstResponder];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

fieldToEnterText = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-30, 30)];
fieldToEnterText.borderStyle = UITextBorderStyleRoundedRect;
fieldToEnterText.delegate = self;
[fieldToEnterText setKeyboardType:UIKeyboardTypeNumberPad];
[fieldToEnterText setTextAlignment:NSTextAlignmentCenter];

invisibleTextfield = [[UITextField alloc] init];
[invisibleTextfield setKeyboardType:UIKeyboardTypeNumberPad];
invisibleTextfield.inputAccessoryView = [self keyboardToolBar];
invisibleTextfield.delegate = self;
[self.view addSubview:invisibleTextfield];
_textField.text = @"Text";
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Interface

最佳答案

默认情况下,iOS 模拟器已连接硬件键盘。此行为类似于连接蓝牙键盘时设备的行为方式。

您可以点击 cmd-k 来打开/关闭软件键盘,就像您可以使用蓝牙键盘上的弹出按钮一样。

如果要测试断开连接的配置,可以通过shift-cmd-k断开硬件键盘

关于ios - TextField becomeFirstResponder 不显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960550/

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