4

在我的应用程序中,navigationController 工具栏上有一个 UITextField。

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *toolBarButtonItems;
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UITextField *textField2;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    self.textField.delegate = self;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.textField];

    self.toolBarButtonItems = @[flexibleSpace,barButtonItem,flexibleSpace];

    self.toolbarItems = self.toolBarButtonItems;
    self.navigationController.toolbar.barTintColor = [UIColor blueColor];

    [self.navigationController setToolbarHidden:NO animated:NO];
}

单击 textField 时,键盘打开,我创建了一个带有另一个 textField 的新 inputAccessoryView 工具栏。

-(UIToolbar *)addToolBar{
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:self.navigationController.toolbar.frame];
    toolbar.barTintColor = [UIColor darkGrayColor];

    self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    self.textField2.delegate = self;
    self.textField2.borderStyle = UITextBorderStyleRoundedRect;

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.textField2];

    [toolbar setItems:@[flexibleSpace,barButtonItem,flexibleSpace]];
    return toolbar;
}

这个想法是将 firstResponder 更改为 inputAccessoryView 上的 textField ,这样我就可以看到我正在编辑的内容。我这样做的原因是我无法将导航工具栏向上滚动到键盘上方,并且我想查看正在编辑的文本。

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.inputAccessoryView = [self addToolBar];

    if(self.textField2.isFirstResponder != NO){
        [self.textField2 becomeFirstResponder];
    }
}

当我单击 navigationController 工具栏中的 textField 时,它似乎不起作用。新的 inputAccessoryView 工具栏显示在键盘上,但我无法编辑该字段,因为响应者似乎没有改变。返回键也不起作用。我必须按两次才能关闭键盘,当我这样做时,两个文本字段之间的文本不匹配。

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

1 回答 1

1

我让它像这样工作:

#import "KJMViewController.h"

@interface KJMViewController ()

@property (strong, nonatomic) UITextField *textField1;
@property (strong, nonatomic) UITextField *textField2;

@end

@implementation KJMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField1 = [[UITextField alloc]initWithFrame:CGRectMake(30, 7, 260, 30)];
    self.textField1.borderStyle = UITextBorderStyleRoundedRect;
    self.textField1.delegate = self;
    UIToolbar *navToolbar = self.navigationController.toolbar;
    [navToolbar addSubview:self.textField1];

    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(30, 7, 260, 30)];
    self.textField2.borderStyle = UITextBorderStyleRoundedRect;
    self.textField2.delegate = self;
    [toolbar addSubview:self.textField2];
    self.textField1.inputAccessoryView = toolbar;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(firstRes:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)firstRes:(id)sender
{
    [self.textField2 becomeFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.textField2) {
        self.textField1.text = self.textField2.text;
    }
    [textField resignFirstResponder];
    [self.textField1 resignFirstResponder];
    return YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]removeObserver:self forKeyPath:UIKeyboardDidShowNotification];
    [super viewDidDisappear:animated];
}

@end

这是发生的事情viewDidLoad

  • 初始化工具栏和 textField2
  • 在这里设置 textField1 的 inputAccessory(被键盘隐藏的那个),这样它就可以成为 firstResponder

然后在viewDidAppear方法中:

注册在显示键盘时发送的通知。然后,您将在“firstRes”方法中编写一些代码,以使 textField2 成为 firstResponder。您需要使用此通知使其成为 firstResponder,因为您知道此时它在视图层次结构中,这意味着它能够成为 firstResponder。在 textField2 出现在屏幕上之前,调用它-(void)textFieldDidBeginEditing:(UITextField *)textField似乎会触发它,这意味着它不能成为 firstResponder。我们在viewDidAppear方法中注册它,因为我们只想在屏幕上时收到通知。

在 textField2 resignsFirstResponder 之后,textField1 再次成为第一响应者,因此您必须在方法中调用 resignFirstResponder 两次textFieldShouldReturn

另外,如果我们离开屏幕,我们需要在方法中移除自己作为键盘通知的观察者viewDidDisappear

这是我在 Xcode 中创建的项目的链接,以便您了解它是如何工作的:

https://github.com/kylejm/UIToolBar-UITextView

于 2013-12-11T01:46:32.857 回答