Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create action to search button in keyboard?

I have three text fields in one page, when the textfield is empty, then the keyboard should show return keyboard, otherwise it should show search, and by clicking on that search button we need to go to another view controller. can any explain with sample code?

like image 402
King Avatar asked Nov 25 '25 19:11

King


1 Answers

Set delegate of your UITextField and them implement delegate function

To show search and return key

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField.text isEqualToString:@""])
    {
        textField.returnKeyType=UIReturnKeyDefault;
    }
    else{
        textField.returnKeyType=UIReturnKeySearch;
    }
}

Coding on keyboard return button

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType==UIReturnKeyDefault)
    {
        //Your Return Key code
    }
    else if(textField.returnKeyType==UIReturnKeySearch)
    {
        //Your search key code
    }
}
like image 107
Rajneesh071 Avatar answered Nov 28 '25 10:11

Rajneesh071