How can you program a UITextField's keyboard to open on viewDidLoad? - iphone

I want my UITextField's keyboard to stay open for the entire time I use this view controller. I don't want it to only open when my user touches the text field. In order to do this, I was hoping I would call the textFieldShouldBeginEditing method by doing this:
EDIT: thanks everyone, I just noticed I called my UITextField a UIImage field for some reason in the interface.

The textFieldShouldBeginEditing delegate method is not something that you call from your code. The OS calls the method when that particular event occurs, and you put code in there to run when the event is fired (similar to putting code in viewDidLoad for your view controller).
To show the keyboard whenever the view controller appears, simply call the UITextField's becomeFirstResponder method in the view controller's viewDidAppear method like this:
[self.myTextField becomeFirstResponder];
Don't forget to create an IBOutlet parameter for the UITextField, link it in Interface Builder, and replace self.myTextField above with the outlet that you created.

You should trigger your textview in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.myTextField becomeFirstResponder];
}

Related

Call method when changing UITextField's directly

I'm currently developing an iPhone app where I have two UITextField's in one View Controller.
I have a method that is called when the keyboard appears (keyboardWillShow), and one that is called when it disappears (keyboardWillDisappear).
Now if the user touches the first field the keyboardWillShow method is called, but if he now touches the second field without touching the background before, keyboardWillShow of course is not called again, because the keyboard is already here.
I can also not use textFieldDidBeginEditing:(UITextField *)textField because it's also not called when you change field's directly.
Now how can i call keyboardWillShow again if he touches the first and the second one without letting the keyboard disappear between??
I found out the problem was i didn't set the delegate of the textfield's properly.
After setting #interface ViewController : UIViewController <UITextFieldDelegate>in my .h file and textfield2.delegate = self in the .m file, textFieldDidBeginEditing: was called when i changed the textfield's directly.

How can I bring keyboard to edit a field when a UIViewController is shown?

I want to show the keyboard just after a view controller is being pushed to start editing a specific UITextField.
I believe that I should manually fire the event on the ViewDidAppear.
Which is the proper way of doing such tasks?
To make keyboard appear you need to manually set your text field as a first responder:
[textField becomeFirstResponder];
It can be called either in viewWillAppear: or in viewDidAppear: method - whichever provides best behaviour for you.
for that you need IBOutlet UITextFiled *yourTextField;
- (void)viewDidLoad or viewWillAppear:(BOOL)animated or viewDidAppear:(BOOL)animated
{
[yourTextField becomeFirstResponder];
}

SearchBar not working when search button is clicked

Can anyone help me out, i am working with UISearchBar and want that when i write anything in SearchBar and press enter key, it will start desired operation...
I have read searchBarSearchButtonClicked function will get called once the “Search” button will be clicked on the pop-up keyboard window. But it is not called:(
I have implemented as:
(void) searchBarSearchButtonClicked:(UISearchBar*) searchContact
{
//do some thing
}
i have checked by placing breakpoints infact this function is not being called....
any idea??
Thanks!!
Did you assign delegate to that viewcontroller which implemented "searchBarSearchButtonClicked" ?
You need to assign delegate like this.
searchBar.delegate =self;
You need to implement the UISearchBarDelegate protocol and set the delegate of the textfield to the class which implements this protocol.
This is the reason why the delegate is not getting called.
You can try this,
In IB set the delegate to file's owner and implement the delegate.
In Code, if you hold reference to the search bar, then on viewDidLoad
searchBar.delegate = self;

Unable to dismiss keyboard with UITextField

I cannot get textFieldShouldEndEditing to call. I have re created the links in interface builder and tried, but nothing seems to work. Any idea why this would not be called?
Edit
I am implementing both -textFieldShouldEndEditing and -textFieldDidFinishEditing. Adding the delegate for the textField didnt help either. The code I am using below is not triggering the log messages.
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(#"Done editing...");
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"Done editing...");
}
Don't use -textFieldDidEndEditing. That gets called after the text field resigns first responder... which it hasn't yet because you haven't told it to because that method hasn't been called yet because first responder hasn't been resigned because you haven't told it to yet. Circular, chicken-and-egg sort of problem.
The delegate method you want is -textFieldShouldReturn. That gets called when the "return" key is pressed. Inside that, tell the text field to resign first responder, then return YES.
Also put a big clear custom button behind your form, and hook its touch-up-inside to a method that goes through all the text fields in the form and resigns first responder on each of them. So a background touch releases the keyboard too.
I saw it was a long-long time ago and I hope that since then you could solve the problem. Everybody tells you the right thing, but you have to take care of all at the same time! Let me just go through on all of them:
Make the promise first:
#interface yourViewController : UIViewController <UITextFieldDelegate>
Then make sure, you also hook up your textfields' delegates on the storyboard to the actual viewcontroller (yourViewController)!
When its done, implement the method in the right viewcontroller file (yourViewController.m):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Test it, and it should work now...
I hope it helps!
set the delegate to your TextField
In the interface Builder select your TextField, then press command + 2(Connections) and connect the delegate outled with the file's owner then save, and with that should work.
the delegate method are:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
}
Hope this can help you.
Have you set the delegate?
If not,set using
textField.delegate=self;
or else,
You must include UITextFieldDelegate in .h file
#interface yourViewController : UIViewController <UITextFieldDelegate>
EDIT:
You have to code in this delegate textFieldDidEndEditing not this textFieldShouldEndEditing

Unhide the keyboard in iphone

In my application what is want is when user clicks on button next view is pushed and default keyboard should be open in the pushed view .
Thanks in advance
You'll have to use -becomeFirstResponder: in the -viewDidAppear: method.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[yourTextField becomeFirstResponder];
}
This will make the keyboard appear.
Assuming you have a text field or something in that view you need to do this:
[myTextView becomeFirstResponder] in your viewDidLoad method.
in your next view, call the becomeFirstResponder method from the input field.
e.g. [[nextViewController] usernameField] becomeFirstResponder];