Question

I have a simple application which has recently had it's premise changed. It used to be that a user would tap on a text field and up would come a custom toolbar above the keyboard, allowing the user to select from the custom buttons to add to the text in the text field. So if a user typed in John's and pressed the Wedding button, the text field would add Wedding to the end so it would now say John's Wedding.

I have recently changed the UI to incorporate a new Table view controller when the user selects the text field because I have some autocompleting occurring making it easier for the user. I have brought the keyboard forward but now, instead of the custom button adding to the text in the search bar, it is simply replacing it.

So if you type John in the search bar and press Wedding in the custom keyboard, the search bar's text completely gets replaced from John to Wedding. This is not ideal and I need to fix this.

Here is some code:

-(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;

This calls the method below:

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

This used to be:

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

So the searchBar does not have an insertText method and I can understand that it is simply replacing the text over it; however I am not sure how I would go about adding the custom toolbar word to the END (or beginning) of the text in the search bar depending on what the user types.

If the user presses the button before typing, it would be Wedding John and if the user presses it after (more likely) it should be John's Wedding, or whatever the user types.

Any assistance would go a long way!

Was it helpful?

Solution

Maybe I'm misunderstanding, but couldn't you just do this:

NSString *text = self.eventAddSearchBar.text;
self.eventAddSearchBar.text = [text stringByAppendingFormat:@" %@", sender.title];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top