文本字段becomeFirstResponder DOS不显示键盘
在我的计划,我需要证明从文本框,这不是用户在工具栏上的互动,这是键盘附件查看文本。当我按下按钮时,这部分工作。然后,如果设备旋转,我需要隐藏键盘。这部分也工作正常。但是当我回到portret并按下需要显示键盘的按钮时,就没有任何声音。文本字段becomeFirstResponder DOS不显示键盘
这里是我的代码:
@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 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
当我打电话[invisibleTextfield becomeFirstResponder];我想用评估视图来启动键盘,其中包含文本字段。然后在textFieldDidBeginEditing:我改变第一响应者fieldToEnterText – 2014-09-21 15:30:43
[IT这看起来如何(http://i.imgur.com/h7XNijh.png) – 2014-09-21 15:33:31
它不会在这种情况下,因为工作,我还需要设置一些“newTextfield”成为第一响应者。但旋转成为第一个响应方法不起作用,所以键盘将显示,因此选择器不会使方法keyboardMNotification变得方便:和 – 2014-09-22 08:52:45
由于iOS8上,呼吁becomeFirstResponder上一个UITextView上不显示键盘对我来说iOS模拟器,但它可以在真实设备上使用。所以一定要用实际的设备来检查它。
默认情况下,iPhone模拟器具有连接硬件键盘。这种行为与连接蓝牙键盘时设备的行为类似。
您可以点击CMD-k可开启/关闭,就像你可以使用蓝牙键盘上的弹出按钮,软件键盘。
如果要测试断开连接的配置,可以使用shift-cmd -k断开硬件键盘
您是否在模拟器上运行它? – n00bProgrammer 2014-09-21 15:43:24
我尝试了无论是在模拟器和设备 – 2014-09-21 15:54:51