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

