如何启用/禁用UITextField的inputAccessoryView按钮?

时间:2013-12-17 20:17:45

标签: ios objective-c inputaccessoryview

我创建了一个UIToolBar作为inputAccessoryView,其下一个和前一个按钮将在我的视图控制器中循环显示textFields。

我在UITextField上基于third SO answer on this page创建了一个类别,它向textField添加了一个指向下一个/上一个textField的属性。

我可以让它在向前和向后循环遍历textFields,但只能循环一次,然后永久禁用我的按钮。此外,当最后一个textField处于焦点时,我仍然需要再次点击下一个按钮(3个textFields 4个点击)以禁用下一个按钮 - 与上一个按钮相同,我必须在我在第一个textField。

// ViewController.h
@interface DetailViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate> {

    __weak IBOutlet UITextField *valueField;
    __weak IBOutlet UITextField *nameField;
    __weak IBOutlet UITextField *serialNumberField;

}

@property (nonatomic, strong) UITextField *currentTextField;

- (IBAction)nextTextField:(id)sender;
- (IBAction)prevTextField:(id)sender;


// ViewController.m
- (void)viewDidLoad {
    //...

    nameField.delegate = self;
    nameField.nextTextField = serialNumberField;
    nameField.prevTextField = nil;

    serialNumberField.delegate = self;
    serialNumberField.nextTextField = valueField;
    serialNumberField.prevTextField = nameField;

    valueField.delegate = self;
    valueField.prevTextField = serialNumberField;
    valueField.nextTextField = nil;

    //...
}

- (void)viewWillAppear:(BOOL)animated {
    //UIToolBar for inputAccessoryView
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    UIBarButtonItem *nextField = [[UIBarButtonItem alloc] initWithTitle:@"\U00003009"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(nextTextField:)];
    UIBarButtonItem *prevField = [[UIBarButtonItem alloc] initWithTitle:@"\U00003008"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(prevTextField:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(backgroundTapped:)];
    NSArray *toolBarButtons = @[prevField, nextField, space, done];
    toolBar.items = toolBarButtons;

    nameField.inputAccessoryView  = toolBar;
    valueField.inputAccessoryView = toolBar;
    serialNumberField.inputAccessoryView = toolBar;
}

    - (IBAction)nextTextField:(id)sender {
    UITextField *next = self.currentTextField.nextTextField;

    if (!next) {
        [sender setEnabled:NO];
    } else {
        [sender setEnabled:YES];
        [next becomeFirstResponder];
    }
}


- (IBAction)prevTextField:(id)sender {
    UITextField *prev = self.currentTextField.prevTextField;

    if (!prev) {
        [sender setEnabled:NO];
    } else {
        [sender setEnabled:YES];
        [prev becomeFirstResponder];
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题的一部分是你正在处理一个方法中启用/禁用条形按钮的方法,只有在点击其中一个条形按钮后才会调用它。最好将barbuttonitems设置为视图控制器的属性(这样你可以在需要时启用/禁用它们),然后在UITextField的'textFieldShouldBeginEditing'委托方法中处理栏按钮项的启用/禁用。 / p>

所以,像这样:

- (void)viewWillAppear:(BOOL)animated {
    //UIToolBar for inputAccessoryView
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    self.moveToNextFieldButton = [[UIBarButtonItem alloc] initWithTitle:@"\U00003009"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(nextTextField:)];
    self.moveToPrevFieldButton = [[UIBarButtonItem alloc] initWithTitle:@"\U00003008"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:self
                                                                 action:@selector(prevTextField:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(backgroundTapped:)];
    NSArray *toolBarButtons = @[self.moveToPrevFieldButton, self.moveToNextFieldButton, space, done];
    toolBar.items = toolBarButtons;

    nameField.inputAccessoryView  = toolBar;
    valueField.inputAccessoryView = toolBar;
    serialNumberField.inputAccessoryView = toolBar;
}

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

    UITextField *next = textField.nextTextField;
    UITextField *prev = textField.prevTextField;
    self.moveToNextFieldButton.enabled = next != nil;
    self.moveToPrevFieldButton.enabled = prev != nil;

    return YES;
}

- (IBAction)nextTextField:(id)sender {
    UITextField *next = self.currentTextField.nextTextField;

    if (next) {
        [next becomeFirstResponder];
    } 
}


- (IBAction)prevTextField:(id)sender {
    UITextField *prev = self.currentTextField.prevTextField;

    if (prev) {
        [prev becomeFirstResponder];
    }
}