【问题标题】:Show keyboard with textField when pressing uibutton按下 uibutton 时显示带有 textField 的键盘
【发布时间】:2016-03-26 02:39:47
【问题描述】:

我有一个按钮:

@property (weak, nonatomic) IBOutlet UIBarButtonItem *addTaskBtn;

...

// Add new task button action.
- (IBAction)addTaskBtnAction:(id)sender {

}

我希望在按下按钮时弹出带有文本字段辅助工具栏的键盘。该文本字段也应该成为导致某种悖论的第一响应者......

我发现如果不将其作为现有文本文件的第一响应者就无法显示键盘,但我正在尝试创建一个文本字段作为键盘的工具栏。

最好的例子就是在这个应用程序中:https://todoist.com/ - 他们成功地做到了我想要实现的目标。

有什么想法吗?

【问题讨论】:

  • 您可以创建一个隐藏的文本字段,当您想显示键盘时,它将成为第一响应者
  • 我试过了。问题是如果隐藏的文本字段拉起键盘,我如何使连接到所述键盘的工具栏附件成为第一响应者?我

标签: ios objective-c uibutton keyboard


【解决方案1】:

试试这个隐藏文本字段的代码:
将 txt 隐藏为隐藏并使其成为按钮点击的第一响应者

- (IBAction)btnOpenTextfield:(id)sender {
    [self.txtHidden becomeFirstResponder];
}

- (void) setupToolbar {
    UIToolbar *keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 42)];
    self.txtToolbar = [[UITextField alloc] initWithFrame:CGRectMake(8, 6, 250, 30)];
    UIBarButtonItem *textBtn = [[UIBarButtonItem alloc] initWithCustomView:self.txtToolbar ];
    UIBarButtonItem *postBtn = [[UIBarButtonItem alloc]initWithTitle:@"Post" style:UIBarButtonItemStyleBordered target:self action:@selector(postComment)];
    [keyboardToolBar setItems: [NSArray arrayWithObjects:textBtn,postBtn,nil]];
    self.txtHidden.inputAccessoryView = keyboardToolBar;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.txtHidden) {
        [self setupToolbar];
    }
    return true;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [self performSelector:@selector(changeResponder) withObject:nil afterDelay:0.1];
    [self changeResponder];
}

- (void) changeResponder {
    [self.txtToolbar becomeFirstResponder];
}

这不是最好的方法,但你可以试试这个,直到你得到任何其他更好的解决方案

【讨论】:

  • @mic773 ,这对你有帮助吗,请勾选“是”,以便其他人也可以从中受益
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
相关资源
最近更新 更多