IOS 開發(fā)UITextView回收或關(guān)閉鍵盤
IOS 開發(fā)UITextView回收或關(guān)閉鍵盤
iOS開發(fā)中,發(fā)現(xiàn)UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那么要實(shí)現(xiàn)UITextView關(guān)閉鍵盤,就必須使用其他的方法,下面是可以使用的幾種方法。
1.如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個(gè)Done的按鈕,用來(lái)退出鍵盤,當(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;
}
這樣無(wú)論你是使用電腦鍵盤上的回車鍵還是使用彈出鍵盤里的return鍵都可以達(dá)到退出鍵盤的效果。
3.第三種方法感覺效果比上面兩種都好,就是在彈出的鍵盤上面加一個(gè)view來(lái)放置退出鍵盤的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];
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- IOS 中UITextField和UITextView中字符串為空和空格的解決辦法
- IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來(lái)計(jì)算高度
- iOS UITextField、UITextView只限輸入中文、英文、數(shù)字及實(shí)時(shí)限制字符個(gè)數(shù)的封裝實(shí)現(xiàn)代碼
- iOS開發(fā)中Swift3 監(jiān)聽UITextView文字改變的方法(三種方法)
- iOS中的UITextView文字輸入光標(biāo)使用技巧小結(jié)
- iOS應(yīng)用開發(fā)中的文字選中操作控件UITextView用法講解
- iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼
相關(guān)文章
C++實(shí)現(xiàn)公司人事管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)公司人事管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
剖析C++中的常量表達(dá)式與省略號(hào)的相關(guān)作用
這篇文章主要介紹了C++中的常量表達(dá)式與省略號(hào)的相關(guān)作用,以及表達(dá)式中的可變參數(shù)模板示例,需要的朋友可以參考下2016-01-01
C/C++ winsock實(shí)現(xiàn)不同設(shè)備實(shí)時(shí)通訊的示例代碼
這篇文章主要為大家詳細(xì)介紹了C/C++如何利用winsock連接實(shí)現(xiàn)不同設(shè)備實(shí)時(shí)通訊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
c++之std::get_time和std::put_time
std::get_time和std::put_time是C++中用于日期和時(shí)間的格式化和解析的函數(shù),它們都包含在<iomanip>頭文件中,std::get_time用于從輸入流中解析日期時(shí)間字符串,而std::put_time則用于將std::tm結(jié)構(gòu)格式化為字符串2024-10-10
C++實(shí)踐數(shù)組作數(shù)據(jù)成員的參考
今天小編就為大家分享一篇關(guān)于C++實(shí)踐數(shù)組作數(shù)據(jù)成員的參考,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02

