UiOS開發(fā)中ITextView回收或關(guān)閉鍵盤使用方法總結(jié)
iOS開發(fā)中,發(fā)現(xiàn)UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那么要實現(xiàn)UITextView關(guān)閉鍵盤,就必須使用其他的方法,下面是可以使用的幾種方法。
1.如果你程序是有導(dǎo)航條的,可以在導(dǎo)航條上面加多一個Done的按鈕,用來退出鍵盤,當(dāng)然要先實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自定義View實現(xiàn)可展開、會呼吸的按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)可展開、會呼吸的按鈕,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
Android控件ViewFlipper仿淘寶頭條垂直滾動廣告條
這篇文章主要為大家詳細(xì)介紹了Android控件ViewFlipper仿淘寶頭條垂直滾動廣告條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Android TimerTask 的簡單應(yīng)用及注意事項
這篇文章主要介紹了Android TimerTask 的簡單應(yīng)用及注意事項的相關(guān)資料,需要的朋友可以參考下2017-06-06
詳解Android權(quán)限管理之RxPermission解決Android 6.0 適配問題
本篇文章主要介紹了Android權(quán)限管理之RxPermission解決Android 6.0 適配問題,具有一定的參考價值,有需要的可以了解一下。2016-11-11
Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例
本篇文章主要介紹了Android帶圓形數(shù)字進(jìn)度的自定義進(jìn)度條示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android實現(xiàn)的狀態(tài)欄定制和修改方法
這篇文章主要介紹了Android實現(xiàn)的狀態(tài)欄定制和修改方法,涉及Android針對狀態(tài)欄屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android 列表倒計時的實現(xiàn)的示例代碼(CountDownTimer)
本篇文章主要介紹了Android 列表倒計時的實現(xiàn)的示例代碼(CountDownTimer),具有一定的參考價值,有興趣的可以了解一下2017-09-09

