1

I have an app where I want a user to tap on a text field, type in their search query, and hit the keyboard's Return (or here, Search) button. Doing so should trigger a segue, and send some data in the prepareForSegue method to the next controller.

This method is currently being triggered by a UIButton, and it works fine, but I'd like the exact same thing to happen when hitting Search on the keyboard, so I can remove the button and streamline the process.

I've tried calling [self prepareForSegue] in the textFieldShouldReturn method (of the delegate) but it hasn't worked.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString: @"DisplaySearchResults"]) {
        SearchResultsViewController *searchResultsViewController = [segue destinationViewController];
        searchResultsViewController.searchQuery = _searchQuery;
    }
}

1 Answer 1

4

You should call

[self performSegueWithIdentifier:@"DisplaySearchResults" sender:self];

in textFieldShouldReturn.

prepareForSegue will be called automatically after performSegueWithIdentifier.

2
  • 1
    This will work, just make sure that the parent class is a delegate for that UITextField.
    – LJ Wilson
    Nov 3, 2012 at 14:55
  • You can fire a push segue setup on the text field simply by hiding the keyboard. See stackoverflow.com/questions/13837689/…
    – Brad M
    Dec 2, 2013 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.