gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:50 26 4
gpt4 key购买 nike

我创建了一个 UIToolBar 作为 inputAccessoryView,带有下一个和上一个按钮,这些按钮将在我的 View Controller 中的文本字段中循环。

我根据 third SO answer on this page 在 UITextField 上创建了一个类别它向指向下一个/上一个文本字段的文本字段添加一个属性。

我可以让它向前和向后循环遍历文本字段,但只能循环一次,然后我的按钮将被永久禁用。此外,当最后一个文本字段处于焦点时,我仍然需要额外点击下一个按钮一次(3 个文本字段点击 4 次)以使其禁用下一个按钮 - 与上一个按钮相同,我必须在我点击后退一次'在第一个文本字段中。

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

最佳答案

我认为部分问题在于您正在处理启用/禁用栏按钮的方法,该方法仅在点击其中一个栏按钮后才会调用。最好将 barbuttonitems 设置为 View Controller 的属性(这样您就可以在需要时启用/禁用它们),然后在 UITextField 的“textFieldShouldBeginEditing”委托(delegate)方法中处理栏按钮项的启用/禁用。

所以,像这样:

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

关于ios - 如何启用/禁用 UITextField 的 inputAccessoryView 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com