2

I'm currently loading a -UITextField programmatically and trying to make the keyboard go down once I click on 'Search'.

I declare the -UITextFeild inside the .m file as UITextField *searchTextField;

Inside the .h file I do indeed declare the -UITextField's delegate

@interface firstChannel : UIViewController<LBYouTubePlayerControllerDelegate, UITableViewDelegate,UITableViewDataSource, NSXMLParserDelegate, UITextFieldDelegate, AVPlayerItemOutputPullDelegate>{ 

This is how I add the UITextField programmatically.

 searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)]; searchTextField.borderStyle = UITextBorderStyleRoundedRect; searchTextField.font = [UIFont fontWithName:@"Heiti TC" size:13]; searchTextField.autocorrectionType = UITextAutocorrectionTypeNo; searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; searchTextField.placeholder = @"Search on the Channel"; searchTextField.borderStyle = UITextBorderStyleNone;; searchTextField.textAlignment = UITextAlignmentCenter; searchTextField.center = CGPointMake(160, 43); searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)]; searchTextField.clearButtonMode = UITextFieldViewModeNever; searchTextField.returnKeyType = UIReturnKeySearch; [searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert]; [self.view.window addSubview:searchTextField]; [searchTextField becomeFirstResponder]; 

I added the delegate inside -viewDidLoad

searchTextField.delegate = self; 

However when I click on "Search" on the keyboard nothing happens. I'm unsure why that's happening I've even debugged it by logging something once you hit returns but nothing happens.

6
  • 1
    You never set the text field's delegate. Commented Sep 3, 2013 at 21:53
  • I forgot to mention that inside -viewDidLoad I did add searchTextField.delegate = self; Commented Sep 3, 2013 at 21:56
  • are you using UITextfield or UISeachBar Commented Sep 3, 2013 at 21:59
  • 2
    @GretarAgnarsson Is the call to searchTExtField.delegate made before or after you create and setup the text field? Make sure it is done after. Commented Sep 3, 2013 at 22:01
  • I was calling it before the textField was created, I called it now after it was created and it works, thanks @rmaddy Commented Sep 3, 2013 at 22:05

3 Answers 3

4

Call the searchTExtField.delegate after you create and setup the text field.

Sign up to request clarification or add additional context in comments.

Comments

0

Try moving all the code in viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)]; searchTextField.borderStyle = UITextBorderStyleRoundedRect; searchTextField.font = [UIFont fontWithName:@"Heiti TC" size:13]; searchTextField.autocorrectionType = UITextAutocorrectionTypeNo; searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; searchTextField.delegate = self; searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; searchTextField.placeholder = @"Search on the Channel"; searchTextField.borderStyle = UITextBorderStyleNone;; searchTextField.textAlignment = UITextAlignmentCenter; searchTextField.center = CGPointMake(160, 43); searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)]; searchTextField.clearButtonMode = UITextFieldViewModeNever; searchTextField.returnKeyType = UIReturnKeySearch; [searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert]; [self.view.window addSubview:searchTextField]; [searchTextField becomeFirstResponder]; } 

Also, if you are using search bar then you need to use search bar delegate. If you are just using UITextfield, then you should be okay.

Comments

0

Have you implemented the delegate method (this method gets called when the user taps the return key):

- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; //Perform search with text: [self performSearch:[textField text]]; return NO; } 

This will resign the responder. Here you can also call your search method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.