1

我正在使用以下代码使用 UITextField 委托移动到下一个字段,并且我正在向键盘添加一个带有上一个、下一个和确定按钮的工具栏。代码工作正常。

就像您看到的键盘返回按钮逻辑非常通用,使用 UITextField 标记,这很好,因为我将使用这段代码。现在我需要编写上一个和下一个按钮逻辑,我迷路了。有任何想法吗?

更新(完整的代码,经过一些修改,感谢@8vius 在聊天中花了一些时间与我一起使它工作):

//
// SigninViewController.m
//

#import "SigninViewController.h"

@implementation SigninViewController

@synthesize firstResponder = _firstResponder;
@synthesize toolbar;
@synthesize email;
@synthesize password;

- (void)move:(UIBarButtonItem*)sender {

    NSInteger tag = self.firstResponder.tag;

    if ([sender.title isEqualToString:@"Anterior"]) {
        tag -= 1;
    } else if ([sender.title isEqualToString:@"Próximo"]) {
        tag += 1;
    }

    UITextField *nextTextField = (UITextField*)[self.view viewWithTag:tag];

    if (nextTextField && tag > 0) {
        [nextTextField becomeFirstResponder];
    } else {
        [self.firstResponder resignFirstResponder];
        self.firstResponder = nil;
    }

}

- (void)ok:(id)sender {
    [self.view endEditing:YES];
    self.firstResponder = nil;
}

- (void)textFieldDidBeginEditing:(UITextField*)textField {
    self.firstResponder = textField;
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField {

    NSInteger tag = textField.tag + 1;

    UITextField *nextTextField = (UITextField*)[self.view viewWithTag:tag];

    if (nextTextField) {
        [nextTextField becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
        self.firstResponder = nil;
    }

    return NO;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.email.delegate = self;
    self.password.delegate = self;

    if (self.toolbar == nil)
    {
        self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];

        UIBarButtonItem* previous = [[UIBarButtonItem alloc] initWithTitle:@"Anterior" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
        UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"Próximo" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
        UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
        UIBarButtonItem* ok = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(ok:)];

        [self.toolbar setItems:[[NSArray alloc] initWithObjects:previous, next, space, ok, nil]];

        [self.toolbar setTranslucent:YES];
        [self.toolbar setTintColor:[UIColor blackColor]];
    }

    for (UIView* view in self.view.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            [(UITextField*)view setInputAccessoryView:toolbar];
        }
    }

}

- (void)viewDidUnload {
    self.email = nil;
    self.password = nil;
    [super viewDidUnload];
}

@end
4

1 回答 1

2

这很简单,当您加载视图时,tag根据您希望它们的顺序在文本字段上设置属性,然后您只需遍历字段上的标记元素:

- (void)toggleTextfield:(UIBarButtonItem *)sender {
  NSInteger nextTag = self.firstResponder.tag;
  if ([sender.title isEqualToString:@"Previous"] && nextTag > 1) {
    nextTag -= 1
  } else if ([sender.title isEqualToString:@"Next"]) {
    nextTag += 1;
  }

  UITextField *nextTextField = (UITextField *)[self.view viewWithTag:nextTag];
  if (nextTextField) {
    [nextTextField becomeFirstResponder];
  }
}

并跟踪谁是第一响应者:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
  self.firstResponder = textField;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];
  self.firstResponder = nil;
  return YES;
}

当您加载视图时,将按钮绑定到切换操作:

  UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self
                                                                    action:@selector(toggleTextfield:)];
  UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self
                                                                    action:@selector(toggleTextfield:)];

例如,在我的例子中,我在表格视图中设置了我的文本字段,所以在我的cellForRowAtIndexPath方法中我将tag属性设置rowindexPath.

编辑:您必须设置firstResponder属性才能使其工作。

在您的 .h 文件中:

@property UIView *firstResponder

在您的 .m 文件中:

@synthesize firstResponder = _firstResponder;
于 2012-10-11T03:45:38.313 回答