UITextView回收或關閉鍵盤,uitextview回收鍵盤

來源:互聯網
上載者:User

UITextView回收或關閉鍵盤,uitextview回收鍵盤

  iOS開發中,發現UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那麼要實現UITextView關閉鍵盤,就必須使用其他的方法,下面是可以使用的幾種方法。

1.如果你程式是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實UITextViewDelegate。

- (void)textViewDidBeginEditing:(UITextView *)textView {  
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];
self.navigationItem.rightBarButtonItem = done;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}

- (void)leaveEditMode {
[self.textView resignFirstResponder];
}

2.如果你的textview裡不用斷行符號鍵,可以把斷行符號鍵當做退出鍵盤的響應鍵。
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

這樣無論你是使用電腦鍵盤上的斷行符號鍵還是使用彈出鍵盤裡的return鍵都可以達到退出鍵盤的效果。
3.第三種方法感覺效果比上面兩種都好,就是在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];  
[topView setBarStyle:UIBarStyleBlack];

UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];

UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];

NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
[doneButton release];
[btnSpace release];
[helloButton release];

[topView setItems:buttonsArray];
[tvTextView setInputAccessoryView:topView];

-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}
  
 


 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.