在我的程序中,我需要显示文本字段中的文本,这在工具栏中不是用户交互式的,这是键盘的附件视图.按下按钮时此部分有效.然后如果设备旋转我需要隐藏键盘.这部分也很好.但是当我回到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
Run Code Online (Sandbox Code Playgroud)

默认情况下,iOS模拟器已连接硬件键盘.此行为类似于连接蓝牙键盘时设备的行为方式.
您可以点击cmd-k来打开/关闭软件键盘,就像使用蓝牙键盘上的弹出按钮一样.
如果要测试断开连接的配置,可以使用shift-cmd-k断开硬件键盘的连接
| 归档时间: |
|
| 查看次数: |
4821 次 |
| 最近记录: |