工具栏的上一个和下一个按钮逻辑 [英] toolbar previous and next button logic

查看:75
本文介绍了工具栏的上一个和下一个按钮逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过UITextField委托移至下一个字段,并且还将带有上一个,下一个和确定按钮的工具栏添加到键盘上。代码运行正常。

I'm using the following code to move to the next field using the UITextField delegate and also I'm adding a toolbar to the keyboard with the previous, next and ok buttons. The code is working fine.

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

Like you see the keyboard return button logic is pretty generic, using the UITextField tags, and that's good because I'm gonna use the piece of code all around. Now I will need to write the previous and next buttons logic, and I'm lost. Any ideas?

更新(完整的代码,进行了一些修改,这要感谢@ 8vius在聊天中花了我一些时间来完成它工作):

UPDATE (complete code, with some modifications, thanks to @8vius that spent some time with me in the chat to make it work):

//
// 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


推荐答案

这非常简单,当您加载视图时,根据想要输入的顺序在文本字段上设置 tag 属性,然后只需遍历字段上的标签元素:

It's quite simple, when you load your view you set the tag property on your text fields depending on the order you want them in, then you have to just traverse the tag element on the fields:

- (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];
  }
}

并跟踪谁是第一响应者:

And keep track of who is the first responder:

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

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

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

And when you load your view, you bind the buttons to the toggle action:

  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 indexPath 的值。

In my case, for instance, I set up my text fields inside a table view, so in my cellForRowAtIndexPath method I set the tag property to be the row of the indexPath.

编辑:您必须设置 firstResponder 属性。

在您的.h文件中:

@property UIView *firstResponder

在.m文件中:

@synthesize firstResponder = _firstResponder;

这篇关于工具栏的上一个和下一个按钮逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆