工具栏上一个和下一个按钮逻辑

时间:2012-10-11 03:38:05

标签: ios xcode cocoa-touch

我正在使用以下代码使用UITextField委托移动到下一个字段,并且我正在使用previous,next和ok按钮向键盘添加工具栏。代码工作正常。

就像你看到键盘返回按钮逻辑非常通用,使用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

1 个答案:

答案 0 :(得分: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属性设置为row的{ {1}}。

编辑:您必须设置indexPath属性才能生效。

在你的.h文件中:

firstResponder

在您的.m文件中:

@property UIView *firstResponder