UiOS開發(fā)中ITextView回收或關(guān)閉鍵盤使用方法總結(jié)
iOS開發(fā)中,發(fā)現(xiàn)UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那么要實(shí)現(xiàn)UITextView關(guān)閉鍵盤,就必須使用其他的方法,下面是可以使用的幾種方法。
1.如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個Done的按鈕,用來退出鍵盤,當(dāng)然要先實(shí)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里不用回車鍵,可以把回車鍵當(dāng)做退出鍵盤的響應(yīng)鍵。
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
這樣無論你是使用電腦鍵盤上的回車鍵還是使用彈出鍵盤里的return鍵都可以達(dá)到退出鍵盤的效果。
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]; }
以上所述是小編給大家介紹的UITextView回收或關(guān)閉鍵盤使用方法總結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android學(xué)習(xí)之AppWidget筆記分享
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)筆記之AppWidget的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08Android嵌套滾動NestedScroll的實(shí)現(xiàn)了解一下
嵌套滾動已經(jīng)算一個比較常見的特效了,這篇文章主要介紹了Android嵌套滾動NestedScroll的實(shí)現(xiàn)了解一下,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Android控件之ImageView用法實(shí)例分析
這篇文章主要介紹了Android控件之ImageView用法,以實(shí)例形式較為詳細(xì)的分析了ImageView控件用于顯示圖片的使用方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09Android開發(fā)中Intent.Action各種常見的作用匯總
今天小編就為大家分享一篇關(guān)于Android開發(fā)中Intent.Action各種常見的作用匯總,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Android開發(fā)Kotlin語言協(xié)程的依賴及使用示例
這篇文章主要為大家介紹了Android開發(fā)Kotlin語言協(xié)程的依賴及使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08使用RecyclerView實(shí)現(xiàn)水平列表
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實(shí)現(xiàn)水平列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Android 雙進(jìn)程守護(hù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 雙進(jìn)程守護(hù)的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08