I am creating a sample web application on my iphone... In that application the main page is login page....Users enter their user name and password in the textfield (UITextField), it will verified by program.
My problem is I want to move cursor from One uitextfield to another uitextfield using tab key pressed in the keyboard (Like a windows operation).
How can I do this?
You can move the focus to a particular UITextField by calling:
[myTextField becomeFirstResponder]
If you implement the UITextFieldDelegate in your class by declaring the following method you can handle the "Done" key being pressed on the keyboard, which is the iPhone native app equivalent of pressing tab.
/*****************************************************************************/
/* Handle the Done button being pressed on the keyboard for any of our */
/* editable UITextFields. We either pass the keyboard focus to the next */
/* text field, or we hide the keyboard. */
/*****************************************************************************/
- (BOOL)textFieldShouldReturn:(UITextField *)xiTextField
{
if (xiTextField == myAccountNameTextField])
{
NSLog(#"Done pressed whilst in account name field, pass focus");
[myPasswordTextField becomeFirstResponder];
}
else
{
NSLog(#"No next text field, closing keyboard");
[xiTextField resignFirstResponder];
}
}
Related
I'm working on a TODO-app for IOS with objective C and last version of xcode.
My problem occur when I try to add a new category group (pressing on a floating plus button):
the first time keyboard and the view above (that contains textview and a "arrow up-done button") show up correctly (and dismiss correctly after I click on done button) but when I try again to add a category group by clicking on floating button ,the keyboard and the relative view don't show up. (so keyboard appear only once correctly).
Some code:
- (void) viewDidLoad
{
[Super viewDidLoad];
_GroupTextView.delegate=self;
}
-(IBAction) PlusButtonPressed: (UIButton *) sender
{
_obscureView.hidden= false;
_ViewKeyboard.hidden=false;
_GroupTextView.inputAccessoryView = _VievKeyboard;
[_GroupTextView becomeFirstResponder];
}
-(IBAction)DonePressed:(UIButton *) Sender
{
_obscureView.hidden= True;
_ViewKeyboard.hidden= True;
[_GroupTextView resignFirstResponder];
}
some photos:
Before press plus button for add category group
after I've clicked on plus button
When I click again on plus button
i've checked and the view can become first responder, but keyboard with custom view don't show up.
I have a login view controller with 2 text fields for username and password entry.
Once the user is done entering their password in the password text field, they press the "Done" return key on the keyboard and my IBAction is performed and tries to log them in.
However, if the login is unsuccessful I show them a UIAlertView warning that tells them it failed and then they press "OK" to dismiss the alert view.
I would like to make is to that when they press "OK" to dismiss the UIAlertView, the password text field's keyboard will pop up again / reappear so they can try entering in their password again.
Is there a way to programmatically call a certain text field's keyboard to popup?
Thanks.
EDIT: The keyboard is now successfully popping up using this code:
[_passwordEntry becomeFirstResponder];
but I would like to make it so that it only pops up when the user taps the "OK" button on the UIAlertView.
Is there a way to "listen" for the UIAlertView's "cancelButtonTitle" or dismissal and call my keyboard method when that happens?
Just got it working with the following code:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//u need to change 0 to other value(,1,2,3) if u have more buttons.then u can check which button was pressed.
if (buttonIndex == 0) {
[_passwordEntry becomeFirstResponder];
}
}
Taken from this SO Answer: https://stackoverflow.com/a/6875248/3344977
iOS6
I have 6 UITextFields in a Scene with a Next Button that Segue's into the next Scene.
The code below works great when I hit the Done button on the keyboard:
- (IBAction)dismissKeyboard:(id)sender
{
if (sender == LocationName) {
self.meeting.LocationName = LocationName.text;
}
if (sender == LocationAddress) {
self.meeting.LocationAddress = LocationAddress.text;
}
if (sender == LocationCity) {
self.meeting.LocationCity = LocationCity.text;
}
//I have 3 more text fields after and then I persist to CoreData:
NSError *error = nil;
if (![managedObjectContext save:&error]) {
}
[sender endEditing:YES];
}
If the user hits the Done Button on the Keyboard, the data saves fine.
However, if the User hits the Next Button on the Navigation Bar, without hitting the Done button on the keyboard first, the user Typed inside the UITextfield DOES NOT SAVE. I would like for the user typed Data (Data that user inputted from the keyboard) in all the fields to be saved when the user hits the Next Button on the Navigation Bar to call the next scene.
I have the following code for the Next Button:
- (IBAction)nextButtonPressed:(id)sender {
[self dismissKeyboard:sender];
}
I know the code for nextButtonPressed is wrong. I think I need help with identifying which UITextField called the Keyboard to be visible, and then call the dismissKeyboard with passing the Sender as a parameter.
Thanks
Make use of the UITextField delegate method textFieldDidEndEditing: to know when the focus leaves a text field. This is when you should save its data. This will be called when focus moves to another text field or when the keyboard is completely dismissed.
Your implementation of nextButtonPressed: should simply call becomeFirstResponder on whatever the next text field is. That's it. By making another text field the first responder, the previous one will have its textFieldDidEndEditing: delegate called.
Update:
// This assumes the LocationXXX are instance variables referencing the UITextFields
- (IBAction)nextButtonPressed:(id)sender {
if (sender == LocationName) {
[LocationAddress becomeFirstResponder];
} else if (sender == LocationAddress) {
[LocationCity becomeFirstResponder];
// add the rest as needed
}
}
How do you programmatically set focus to some UITextField in iOS?
When you tap on a textfield, the keyboard pops up to begin editing the textfield.
How can you give focus to a textfield and bring up the keyboard without requiring the user to tap on the textfield?
Make the textField the first responder; this will also popup the keyboard:
[self.textField becomeFirstResponder];
And just some more typical example code which may help someone,
in your storyboard, click on one of the text fields, and set the outlets-delegate to be the class in question. In that class, in the .h file make sure that you declare that it is a UITextFieldDelegate
#interface Login : UIViewController <UITextFieldDelegate>
Then in the .m file, you could have this for example...
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( textField == self.loginEmail ) { [self.loginPassword becomeFirstResponder]; }
if ( textField == self.loginPassword ) { [self yourLoginRoutine]; return YES; }
return YES;
}
When a user clicks the "Next" button on the virtual keyboard - on the 'email' field - it will correctly move you to the password field. When a user clicks the "Next" button on the virtual keyboard - on the 'password' field - it will correctly call your yourLoginRoutine.
(On the storyboard, on the attributes inspector, notice that you can select the text you want on the Return key ... here "Next" would work nicely on the email field and "Done" would work nicely on the password field.)
For changing focus on the fly:
[yourTextField becomeFirstResponder];
For Swift, you can use:
theTextFieldYouWant.becomeFirstResponder()
now i have a textField.
and i enter some words,the keyboard will appear.
when i pressed the keyboard "done",the keyboard will disappear. (i have finished this function)
but next,
i want to insert data using core data framework when the user pressed the button "done"
so,how to solve this problem?
i know how to insert data using core data,so you do not need to tell me how to insert the data.
i am using this code to disappear keyboard.
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[txtName resignFirstResponder];
return YES;
}
thanks everybody
In the example code, I have 3 UITextField. You can process your update after we reach the last field
// I the tag property to indicate which field I am on during runtime
enum {
Line1Tag = 50,
Line2Tag,
Line3Tag
};
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// The user has pressed the "Return Key"
// Which I have set to "Next" for first two lines
// and "Done" for the last line, so jump to the next text field
NSLog(#"\"Return\" key pressed.");
// based on which text field we are in jump to the next
if (textField.tag == Line3Tag)
// We have reach the last line so hide keyboard
[textField resignFirstResponder];
// this is where you can perform Core Data updates if you like
else {
int nextTag = textField.tag + 1;
UIView *nextField = [self.view viewWithTag:nextTag];
[nextField becomeFirstResponder];
// Once the next text field is the first responder
// I need to make sure the user can see it
[self makeActiveTextFieldVisible];
}
return NO;
}