【问题标题】:Custom Keyboard Text Fully Replacing The Text in a UISearchBar rather than Adding to it [iOS]自定义键盘文本完全替换 UISearchBar 中的文本,而不是添加到其中 [iOS]
【发布时间】:2014-03-06 07:01:20
【问题描述】:

我有一个简单的应用程序,最近更改了它的前提。过去,用户点击文本字段,键盘上方会出现一个自定义工具栏,允许用户从自定义按钮中进行选择以添加到文本字段中的文本。因此,如果用户输入 John's 并按下 Wedding 按钮,则文本字段会将 Wedding 添加到末尾,因此现在会显示 John's Wedding。

我最近更改了 UI,以便在用户选择文本字段时合并一个新的表格视图控制器,因为我有一些自动完成功能,使用户更容易。我已经将键盘向前推进了,但现在,不是将自定义按钮添加到搜索栏中的文本,而是简单地替换它。

因此,如果您在搜索栏中键入 John 并在自定义键盘中按 Wedding,则搜索栏的文本会完全从 John 替换为 Wedding。这并不理想,我需要解决这个问题。

这是一些代码:

-(void)configureKeyboardToolbars
{
    UIToolbar *eventToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                                          self.view.window.frame.size.width, 44.0f)];
    eventToolBar.tintColor = [UIColor colorWithRed:0.6f green:0.6f blue:0.64f alpha:1.0f];
    eventToolBar.translucent = NO;
    eventToolBar.items =   @[ [[UIBarButtonItem alloc] initWithTitle:@"Wedding"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(barButtonAddText:)],

    // With the text field, the line below used to be:     self.eventTextField.inputAccessoryView = eventToolBar;

    self.eventAddSearchBar.inputAccessoryView = eventToolBar;

这会调用下面的方法:

// A method that responds to the tool bar buttons being pressed
-(IBAction)barButtonAddText:(UIBarButtonItem*)sender
{
    if (self.eventAddSearchBar.isFirstResponder)
    {
        self.eventAddSearchBar.text = sender.title;
    }
}

这曾经是:

// A method that responds to the tool bar buttons being pressed
-(IBAction)barButtonAddText:(UIBarButtonItem*)sender
{
    if (self.eventTextField.isFirstResponder)
    {
        [self.eventTextField insertText:sender.title];
    }
}

所以 searchBar 没有 insertText 方法,我可以理解它只是替换它上面的文本;但是我不确定如何根据用户键入的内容将自定义工具栏单词添加到搜索栏中文本的结尾(或开头)。

如果用户在键入之前按下按钮,则为“婚礼约翰”,如果用户在(更可能)之后按下该按钮,则应为“约翰的婚礼”或用户键入的任何内容。

任何帮助都会大有帮助!

【问题讨论】:

    标签: ios keyboard uibutton uisearchbar uitoolbar


    【解决方案1】:

    也许我理解错了,但你不能这样做吗:

    NSString *text = self.eventAddSearchBar.text;
    self.eventAddSearchBar.text = [text stringByAppendingFormat:@" %@", sender.title];
    

    【讨论】:

    • 哦,伙计,我为这样一个愚蠢的问题道歉。那绝对做到了!非常感谢那里的帮助,这不应该是我花那么多时间做的事情!再次感谢您的帮助,我很抱歉这是非常基本的!
    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2016-03-21
    • 1970-01-01
    • 2022-01-12
    • 2012-08-26
    相关资源
    最近更新 更多