Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to let a user hit the NEXT key and jump to the next `UITextField` within a ScrollView
    primarykey
    data
    text
    <p>I have several <code>UITextFields</code>, my keyboard has a "NEXT" key as the return key. I'd like to let the user hit the NEXT key and jump to the next <code>UITextField</code>. I read online that the best way to do that would be to implement the function:</p> <pre><code>-(BOOL)textFieldShouldReturn:(UITextField *)textField </code></pre> <p>I did this however it is not working for me. </p> <p>Please see my .m file</p> <pre><code>#import "customerInfoViewController.h" @implementation customerInfoViewController @synthesize infoModel; @synthesize Name; @synthesize AptNum; @synthesize Street1; @synthesize Street2; @synthesize City; @synthesize Telephone; @synthesize Email1; @synthesize textFieldBeingEdited; @synthesize scrollView; @synthesize doneButton; CartSingleton *Cart; //============================================================================== -(void)viewWillAppear:(BOOL)animated { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil/*self.view.window*/]; [super viewWillAppear:animated]; Cart = [CartSingleton getSingleton]; } //============================================================================== -(void)viewWillDisappear:(BOOL)animated { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [super viewWillDisappear:animated]; } //============================================================================== - (IBAction)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); textFieldBeingEdited = textField; } //============================================================================== -(IBAction)textFieldDoneEditing:(id)sender { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); textFieldBeingEdited = NULL; [sender resignFirstResponder]; if (moveViewUp) { [self scrollTheView:NO]; } } //============================================================================== -(BOOL)textFieldShouldReturn:(UITextField *)textField { NSLog(@"TextField tag is, %d ", textField.tag); if ([self.Name isFirstResponder]) { [self.AptNum becomeFirstResponder]; } if ([self.AptNum isFirstResponder]) { [self.Street1 becomeFirstResponder]; } if ([self.Street1 isFirstResponder]) { [self.Street2 becomeFirstResponder]; } if ([self.Street2 isFirstResponder]) { [self.City becomeFirstResponder]; } if ([self.City isFirstResponder]) { [self.Telephone becomeFirstResponder]; } if ([self.Telephone isFirstResponder]) { [self.Email1 becomeFirstResponder]; } if ([self.Email1 isFirstResponder]) { [self.Email1 resignFirstResponder]; } return YES; } //============================================================================== -(void)keyboardWillShow:(NSNotification *)notif { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); NSDictionary *info = [notif userInfo]; NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey]; //NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; CGSize keyboardSize = [aValue CGRectValue].size; float bottomPoint = (textFieldBeingEdited.frame.origin.y + textFieldBeingEdited.frame.size.height /*+ 20*/); scrollAmount = keyboardSize.height - (self.view.frame.size.height- bottomPoint); /* CGRect viewFrame = self.view.frame; viewFrame.size.height += keyboardSize.height; scrollView.frame = viewFrame; */ if(scrollAmount &gt; 0) { moveViewUp = YES; [self scrollTheView:YES]; } else { moveViewUp = NO; } } //============================================================================== -(void)scrollTheView:(BOOL)movedUp { NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; CGRect rect = self.view.frame; if(movedUp) { rect.origin.y -=scrollAmount; } else { rect.origin.y +=scrollAmount; } self.view.frame = rect; [UIView commitAnimations]; } //============================================================================== - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); scrollView.contentSize = self.view.frame.size; textFieldBeingEdited = NULL; //If there is already if (Cart.customerInfoObtained) { self.Name.text = Cart.customerInfo.Name; self.AptNum.text = Cart.customerInfo.AptNo; self.Street1.text = Cart.customerInfo.Street1; self.Street2.text = Cart.customerInfo.Street2; self.City.text = Cart.customerInfo.City; self.Telephone.text = Cart.customerInfo.Tel; self.Email1.text = Cart.customerInfo.Email; } } //============================================================================== -(IBAction)doneButtonPressed { NSLog(@"Done button pressed "); NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); [self dismissModalViewControllerAnimated:YES]; /* infoModel = [[customerInfoModel alloc] initWithObjects:self.Name.text AptNo:self.AptNum.text Street1:self.Street1.text Street2:self.Street2.text City:self.City.text Tel:self.Telephone.text Email:self.Email1.text]; [Cart addCustomerInfo:infoModel]; [self.navigationController popViewControllerAnimated:YES]; */ } //============================================================================== - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); // Release any cached data, images, etc. that aren't in use. } //============================================================================== - (void)viewDidUnload { [super viewDidUnload]; NSLog(@"%s %d %s", __FILE__, __LINE__, __FUNCTION__); // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } //============================================================================== - (void)dealloc { NSLog(@"deallocating %@",self); [scrollView release]; [super dealloc]; } //============================================================================== @end </code></pre> <p>I am stepping through the function:</p> <pre><code>-(BOOL)textFieldShouldReturn:(UITextField *)textField </code></pre> <p>and here is the Consoleoutput</p> <pre> TextField tag is, 6 [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload