从inputAccessoryView上的UITextField镜像文本 - UIToolBar到navigationController.toolbar上UITextField上的文本

时间:2013-10-24 22:22:48

标签: ios7 uitextfield uitoolbar uitextfielddelegate uitoolbaritem

在我的应用程序中,我在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;
}

1 个答案:

答案 0 :(得分: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。在-(void)textFieldDidBeginEditing:(UITextField *)textField中调用它似乎在textField2出现在屏幕上之前触发它,这意味着它不能成为firstResponder。我们使用viewDidAppear方法注册,因为我们只想在屏幕上显示通知。

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

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

这是我在Xcode中制作的项目的链接,因此您可以看到它是如何工作的:

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