#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,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,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,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;}解决方法 我让它像这样工作:
#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,320,44)]; self.textFIEld2 = [[UITextFIEld alloc]initWithFrame:CGRectMake(30,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
总结以上是内存溢出为你收集整理的ios7 – 在inputAccessoryView上将来自UITextField的文本 – UIToolBar镜像到navigationController.toolbar上的UITextField上的文本全部内容,希望文章能够帮你解决ios7 – 在inputAccessoryView上将来自UITextField的文本 – UIToolBar镜像到navigationController.toolbar上的UITextField上的文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)