我如何通过iPhone键盘上的“下一步”按钮浏览所有的文本字段?
最后一个文本字段应该关闭键盘。
我已经设置了IB按钮(下一步/完成),但现在我卡住了。
我实现了textFieldShouldReturn动作,但现在下一步和完成按钮关闭键盘。
我如何通过iPhone键盘上的“下一步”按钮浏览所有的文本字段?
最后一个文本字段应该关闭键盘。
我已经设置了IB按钮(下一步/完成),但现在我卡住了。
我实现了textFieldShouldReturn动作,但现在下一步和完成按钮关闭键盘。
当前回答
我已经添加到PeyloW的答案,以防你想实现一个previous/next按钮功能:
- (IBAction)moveThroughTextFields:(UIBarButtonItem *)sender
{
NSInteger nextTag;
UITextView *currentTextField = [self.view findFirstResponderAndReturn];
if (currentTextField != nil) {
// I assigned tags to the buttons. 0 represent prev & 1 represents next
if (sender.tag == 0) {
nextTag = currentTextField.tag - 1;
} else if (sender.tag == 1) {
nextTag = currentTextField.tag + 1;
}
}
// Try to find next responder
UIResponder* nextResponder = [self.view viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
// I added the resign here in case there's different keyboards in place.
[currentTextField resignFirstResponder];
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[currentTextField resignFirstResponder];
}
}
你像这样子类化UIView:
@implementation UIView (FindAndReturnFirstResponder)
- (UITextView *)findFirstResponderAndReturn
{
for (UITextView *subView in self.subviews) {
if (subView.isFirstResponder){
return subView;
}
}
return nil;
}
@end
其他回答
我喜欢Anth0和Answerbot已经提出的面向对象解决方案。然而,我正在开发一个快速而小型的POC,所以我不想让子类和类别使事情变得混乱。
另一个简单的解决方案是创建一个字段的NSArray,并在按下next时查找下一个字段。不是面向对象的解决方案,而是快速、简单且易于实现。此外,您可以一目了然地查看和修改排序。
下面是我的代码(基于这个线程中的其他答案):
@property (nonatomic) NSArray *fieldArray;
- (void)viewDidLoad {
[super viewDidLoad];
fieldArray = [NSArray arrayWithObjects: firstField, secondField, thirdField, nil];
}
- (BOOL) textFieldShouldReturn:(UITextField *) textField {
BOOL didResign = [textField resignFirstResponder];
if (!didResign) return NO;
NSUInteger index = [self.fieldArray indexOfObject:textField];
if (index == NSNotFound || index + 1 == fieldArray.count) return NO;
id nextField = [fieldArray objectAtIndex:index + 1];
activeField = nextField;
[nextField becomeFirstResponder];
return NO;
}
I always return NO because I don't want a line break inserted. Just thought I'd point that out since when I returned YES it would automatically exit the subsequent fields or insert a line break in my TextView. It took me a bit of time to figure that out. activeField keeps track of the active field in case scrolling is necessary to unobscure the field from the keyboard. If you have similar code, make sure you assign the activeField before changing the first responder. Changing first responder is immediate and will fire the KeyboardWasShown event immediately.
在textFieldShouldReturn你应该检查你目前的文本域不是最后一个,当他们点击下一步,如果不是不要解散键盘..
我尝试使用一种更复杂的方法来解决这个问题,该方法基于为UITableView中的每个单元格(或UITextField)分配一个稍后可以检索的唯一标签值: activate-next-uitextfield-in-uitableview-ios
我希望这能有所帮助!
我在我的故事板中有大约10+ UITextField,我启用下一个功能的方式是通过创建一个UITextField数组,并使下一个UITextField成为firstResponder。下面是实现文件:
#import "RegistrationTableViewController.h"
@interface RegistrationTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *fullNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *address2TextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (weak, nonatomic) IBOutlet UITextField *zipCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *urlTextField;
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *confirmPWTextField;
@end
NSArray *uiTextFieldArray;
@implementation RegistrationTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"view did load");
uiTextFieldArray = @[self.fullNameTextField,self.addressTextField,self.address2TextField,self.cityTextField,self.zipCodeTextField,self.urlTextField,self.usernameTextField,self.emailTextField,self.passwordTextField,self.confirmPWTextField];
for(UITextField *myField in uiTextFieldArray){
myField.delegate = self;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
long index = [uiTextFieldArray indexOfObject:textField];
NSLog(@"%ld",index);
if(index < (uiTextFieldArray.count - 1)){
[uiTextFieldArray[++index] becomeFirstResponder];
}else{
[uiTextFieldArray[index] resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我已经实现了在视图层次结构中通过Responders导航的动态解决方案。其思想是,在整个层次结构中搜索下一个可能的响应器,根据需要向前或向后搜索。
UIView搜索扩展:
UIView+Cat.h
- (UIResponder *)getNextFirstResponder;
- (UIResponder *)getPreviousFirstResponder;
UIView+Cat.m
- (UIResponder *)getPreviousFirstResponder {
if (!self.superview) {
return nil;
}
BOOL hasreachedself = NO;
for (NSInteger i = self.superview.subviews.count-1; i >= 0; i--) {
UIView *v = [self.superview.subviews objectAtIndex:i];
if (v == self) {
hasreachedself = YES;
continue;
}
if (!hasreachedself) continue;
if ([v canBecomeFirstResponder] && !v.hidden) {
return v;
}
UIResponder *subResponder = [self getNextFirstResponderInView:v];
if (subResponder) {
return subResponder;
}
}
//search hierachicaly in superviews
return [self.superview getPreviousFirstResponder];
}
- (UIResponder *)getNextFirstResponder {
if (!self.superview) {
return nil;
}
BOOL hasreachedself = NO;
for (UIView *v in self.superview.subviews) {
if (v == self) {
hasreachedself = YES;
continue;
}
if (!hasreachedself) continue;
if ([v canBecomeFirstResponder] && !v.hidden) {
return v;
}
UIResponder *subResponder = [self getNextFirstResponderInView:v];
if (subResponder) {
return subResponder;
}
}
//search hierachicaly in superviews
return [self.superview getNextFirstResponder];
}
- (UIResponder *)getNextFirstResponderInView:(UIView *)view {
if ([view canBecomeFirstResponder] && !view.hidden) {
return view;
}
for (UIView *v in view.subviews) {
UIResponder *subResponder = [self getNextFirstResponderInView:v];
if (subResponder) {
return subResponder;
}
}
return nil;
}
用法:添加UIToolbar到UITextField / UITextView(在类别或子类中):
- (void)addToolbarInputAccessoryView {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
UIBarButtonItem *prev = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"chevron.up"] style:UIBarButtonItemStylePlain target:self action:@selector(moveToPreviousFirstResponder)];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"chevron.down"] style:UIBarButtonItemStylePlain target:self action:@selector(moveToNextFirstResponder)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard)];
toolbar.items = @[prev, next, space, done];
if ([[UITextField appearance] keyboardAppearance] == UIKeyboardAppearanceDark) {
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
[toolbar setBarTintColor:[UIColor blackColor]];
[toolbar setTintColor:[UIColor whiteColor]];
}
[toolbar sizeToFit];
self.inputAccessoryView = toolbar;
}
- (void)hideKeyboard {
[self resignFirstResponder];
}
- (void)moveToNextFirstResponder {
UIResponder *next = [self getNextFirstResponder];
if (next) {
[next becomeFirstResponder];
}
else {
[self resignFirstResponder];
}
}
- (void)moveToPreviousFirstResponder {
UIResponder *prev = [self getPreviousFirstResponder];
if (prev) {
[prev becomeFirstResponder];
}
else {
[self resignFirstResponder];
}
}