gpt4 book ai didi

ios - 自定义键盘文本完全替换 UISearchBar 中的文本而不是添加到它 [iOS]

转载 作者:行者123 更新时间:2023-11-29 10:46:24 25 4
gpt4 key购买 nike

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

我最近更改了 UI 以在用户选择文本字段时合并一个新的 TableView Controller ,因为我有一些自动完成功能可以让用户更轻松。我已经将键盘向前移动,但现在,自定义按钮不再添加到搜索栏中的文本中,而是简单地替换它。

因此,如果您在搜索栏中键入 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 方法,我可以理解它只是替换上面的文本;但是,我不确定如何根据用户键入的内容将自定义工具栏字词添加到搜索栏中文本的结尾(或开头)。

如果用户在键入之前按下按钮,它将是 Wedding John,如果用户在之后(更有可能)按下按钮,则它应该是 John's Wedding,或者用户键入的任何内容。

任何帮助都会大有帮助!

最佳答案

也许我误会了,但你不能这样做吗:

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

关于ios - 自定义键盘文本完全替换 UISearchBar 中的文本而不是添加到它 [iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com