我有几个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
这是Consoleoutput
TextField tag is, 6 [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:] [customerInfoViewController textFieldDidBeginEditing:]
答案 0 :(得分:2)
首先,如果-(BOOL)textFieldShouldReturn:(UITextField *)textField
没有被解雇,首先要确保文本字段有类似name.delegate = self;
其次,如果光标移动到下一个字段,则需要实现
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
此外,如果在显示键盘时滚动视图的一部分位于键盘下方,您将需要缩小滚动视图的高度,以便键盘不会重叠。
答案 1 :(得分:2)
这对我有用:
- (BOOL) textFieldShouldReturn:(UITextField *)textField { // called when "Next" is pressed
NSLog(@"textFieldShouldReturn");
if (textField == nameField) {
[streetAddressField becomeFirstResponder];
} else if (textField == streetAddressField) {
[cityField becomeFirstResponder];
} else if (textField == cityField) {
[stateField becomeFirstResponder];
} else if (textField == stateField) {
[zipField becomeFirstResponder];
}
return YES;
}
答案 2 :(得分:2)
1)符合UITextFieldDelegate
@interface ViewControllerTest :UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txtUserId;
@property (weak, nonatomic) IBOutlet UITextField *txtPassword;
@end
2)将textfield委托分配给当前的viewcontroller
- (void)viewDidLoad
{
[super viewDidLoad];
self.txtUserId.delegate=self;
self.txtUserId.tag=1;
self.txtPassword.delegate=self;
self.txtPassword.tag=2;
}
3)实现textFieldShouldReturn方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField.tag==1)
{
[self.txtPassword becomeFirstResponder];
}
return TRUE;
}