Ios7 将inputAccessoryView-UIToolBar上的UITextField中的文本镜像到navigationController.toolbar上的UITextField中的文本

Ios7 将inputAccessoryView-UIToolBar上的UITextField中的文本镜像到navigationController.toolbar上的UITextField中的文本,ios7,uitextfield,uitoolbar,uitextfielddelegate,uitoolbaritem,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) UITe

在我的应用程序中,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];
}
单击文本字段时,键盘将打开,我使用另一个文本字段创建了一个新的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;
}
想法是将第一响应者更改为inputAccessoryView上的文本字段,这样我就可以看到正在编辑的内容。我这样做的原因是,我无法将导航工具栏向上滚动过键盘,我希望看到我正在编辑的文本

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

    if(self.textField2.isFirstResponder != NO){
        [self.textField2 becomeFirstResponder];
    }
}
当我单击navigationController工具栏中的文本字段时,它似乎不起作用。新的inputAccessoryView工具栏显示在键盘上,但我无法编辑该字段,因为响应程序似乎没有改变。返回键也不起作用。我必须点击两次才能关闭键盘,当我点击时,两个文本字段之间的文本不匹配

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

我让它像这样工作:

#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
中发生的事情:

  • 初始化工具栏和文本字段2
  • 在这里设置textField1(键盘隐藏的那个)的inputAccessory,这样它就可以成为第一响应者了
然后在
视图中显示
方法:

注册显示键盘时发送的通知。然后,您将在“firstRes”方法中编写一些代码,使textField2成为第一响应程序。您需要让它成为使用此通知的第一响应者,因为此时您知道它已在视图层次结构中,这意味着它能够成为第一响应者。在
-(void)textField didbeginediting:(UITextField*)textField
中调用它似乎是在textField2出现在屏幕上之前启动的,这意味着它不能成为第一响应者。我们在
viewdideappear
方法中注册它,因为我们只想在屏幕上收到通知

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

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

这里有一个指向我在Xcode中创建的项目的链接,您可以看到它是如何工作的:


不起作用。现在代码中还有4个文本字段属性。为什么需要另外两个文本字段?您只声明了两个属性,但在代码中使用了4(textField、textField1、textField2、inputf)。还有,为什么要在iOS7中使用iVars呢?哦,对我来说是这样的。我不是有意写“textField”和“inputf”——我在深夜写了代码,只是在写我的答案时将属性名称更改为您正在使用的名称时忽略了它们。属性是IVAR,它们只是用来让Xcode自动生成访问器方法。我现在编辑了我的答案,所以使用了正确的属性名称,希望它现在能为您工作。谢谢,但它似乎无法正常工作。viewDidLoad方法中的self.inputf是什么?此外,代码中不再有navigationController工具栏。你到底为什么要这么做?您是否也将TextField1的inputAccessoryView设置为navigationController工具栏?让我们