在我的程序中,我需要显示textfield中的文本,该文本在工具栏中不是用户交互式的,这是键盘的附件视图。按下按钮时此部分有效。然后如果设备旋转我需要隐藏键盘。这部分也很好。但是当我回到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
答案 0 :(得分:9)
默认情况下,iOS模拟器已连接硬件键盘。此行为类似于设备在连接蓝牙键盘时的行为方式。
您可以点击cmd-k来打开/关闭软件键盘,就像使用蓝牙键盘上的弹出按钮一样。
如果要测试断开连接的配置,可以使用shift-cmd-k
断开硬件键盘答案 1 :(得分:0)
当你按下你拨打的按钮时看起来像
[invisibleTextfield becomeFirstResponder];
但是当您轮换时,请拨打[fieldToEnterText resignFirstResponder];
。
这是故意的吗?为什么不辞去同一文本字段?
更新:
为什么要将文本字段添加到附件视图?我会在屏幕底部创建一个文本视图,并在按下按钮时将其设置为firstresponder。然后捕获KB上滑事件并用它滑动文本视图。
注册参加这些活动:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardMNotification:)
name:UIKeyboardWillHideNotification
object:nil];
在此处查看您可以从此通知获得的值:UIKeyboardWillShowNotification
答案 2 :(得分:0)
从iOS8开始,在UITextView上调用becomeFirstResponder并不会在iOS模拟器上为我显示键盘,但它会在真实设备上显示。因此,请务必使用实际设备进行检查。