Ios TextField becomeFirstResponder不显示键盘
在我的程序中,我需要显示textfield中的文本,这在工具栏中不是用户交互的,它是键盘的附件视图。当我按下按钮时,这部分工作。若设备旋转,我需要隐藏键盘。这部分也很好。但当我回到portret并按下按钮时,需要显示键盘上并没有七分音符 这是我的密码:Ios TextField becomeFirstResponder不显示键盘,ios,ios-simulator,Ios,Ios Simulator,在我的程序中,我需要显示textfield中的文本,这在工具栏中不是用户交互的,它是键盘的附件视图。当我按下按钮时,这部分工作。若设备旋转,我需要隐藏键盘。这部分也很好。但当我回到portret并按下按钮时,需要显示键盘上并没有七分音符 这是我的密码: @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation ViewContro
@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
看起来就像你按下了你呼叫的按钮
[invisibleTextfield成为第一响应者]代码>但当你轮换时,你调用[fieldtoentertextresignfirstresponder]代码>。
这是故意的吗?你为什么不退出同一个文本字段
更新:
为什么要将文本字段添加到附件视图?我会在屏幕底部创建一个文本视图,并在点击按钮时将其设置为firstresponder。然后捕获KB向上滑动事件,并用它滑动文本视图
注册参加这些活动:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillHideNotification
object:nil];
在此处查找可从此通知中获得的值:从iOS8开始,在UITextView上调用becomeFirstResponder不会在iOS模拟器上为我显示键盘,但在真实设备上会显示。所以一定要用实际的设备检查一下 默认情况下,iOS模拟器已连接硬件键盘。此行为类似于连接蓝牙键盘时设备的行为
您可以点击cmd-k打开/关闭软件键盘,就像您可以使用蓝牙键盘上的弹出按钮一样
如果要测试断开连接的配置,可以在我调用[invisibleTextfield becomeFirstResponder]时使用shift-cmd-k断开硬件键盘的连接;我想用包含文本字段的评估视图激活键盘。然后在textFieldDidBeginEditing中:我将first responder更改为fieldToEnterTextIt将不起作用,因为在这种情况下,我还需要将一些“newTextfield”设置为first responder。但是在旋转后becomefirstresponder方法不起作用,所以键盘会显示,所以选择器不会启动方法键盘提示:你在模拟器上运行它吗?我在模拟器和设备上都试过了