Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I enable/disable a UITextField's inputAccessoryView buttons?
    text
    copied!<p>I've created a UIToolBar as an inputAccessoryView with a next and previous buttons that will cycle through the textFields in my view controller.</p> <p>I've created a category on UITextField based on the <a href="https://stackoverflow.com/questions/1347779/how-to-navigate-through-textfields-next-done-buttons">third SO answer on this page</a> which adds a property to the textField that points to the next/previous textField.</p> <p>I can get it to cycle through the textFields both forward and backward, but only once, and then my buttons are permanently disabled. Also, when the last textField is in focus, I still need to tap the next button one extra time (4 taps for 3 textFields) to have it disable the next button — same with the previous button, I have to tap back once when I'm in the first textField. </p> <pre><code>// ViewController.h @interface DetailViewController : UIViewController &lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate&gt; { __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]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload