[英]Mirror text from UITextField on inputAccessoryView - UIToolBar to text on UITextField on navigationController.toolbar
在我的應用程序中,我在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;
}
我讓它像這樣工作:
#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
發生的事情:
然后在viewDidAppear
方法中:
注冊鍵盤顯示時發送的通知。 然后,您將在“firstRes”方法中編寫一些代碼,以使textField2成為firstResponder。 您需要使用此通知將其設置為firstResponder,因為此時您知道它在視圖層次結構中,這意味着它可以成為firstResponder。 在-(void)textFieldDidBeginEditing:(UITextField *)textField
調用它-(void)textFieldDidBeginEditing:(UITextField *)textField
似乎在textField2出現在屏幕上之前觸發它,這意味着它不能成為firstResponder。 我們在viewDidAppear
方法中注冊它,因為我們只想在屏幕上顯示通知。
在textField2 resignsFirstResponder之后,textField1再次成為第一個響應者,因此您必須在textFieldShouldReturn
方法中調用resignFirstResponder兩次。
此外,如果我們離開屏幕,我們需要在viewDidDisappear
方法中刪除自己作為鍵盤通知的觀察者。
這是我在Xcode中創建的項目的鏈接,因此您可以看到它是如何工作的:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.