我有几个UITextFields
,我的键盘上有一个“ NEXT”键作为返回键。我想让用户按下NEXT键并跳到下一个UITextField
。我在网上阅读到,做到这一点的最佳方法是实现该功能:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
我这样做了,但是对我来说不起作用。
请查看我的.m文件
#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 > 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
我正在逐步执行以下功能:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
这是Console输出
TextField标签是6
[customerInfoViewController textFieldDidBeginEditing:]
[customerInfoViewController textFieldDidBeginEditing:]
[customerInfoViewController textFieldDidBeginEditing:]
[customerInfoViewController textFieldDidBeginEditing:]
[customerInfoViewController textFieldDidBeginEditing:]
[customerInfoViewController textFieldDidBeginEditing:]
最佳答案
首先,如果没有触发-(BOOL)textFieldShouldReturn:(UITextField *)textField
,请确保文本字段具有类似name.delegate = self的内容;
其次,如果光标移至下一个字段,则需要实现
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
另外,如果在显示键盘时滚动视图的一部分在键盘下方,则您将希望缩小滚动视图的高度,以使键盘不重叠。