新聞中心
這篇文章主要介紹IOS中開發(fā)UITextView回收或關閉鍵盤的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
IOS 開發(fā)UITextView回收或關閉鍵盤
iOS開發(fā)中,發(fā)現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]; }
以上是“IOS中開發(fā)UITextView回收或關閉鍵盤的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)網站建設公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享標題:IOS中開發(fā)UITextView回收或關閉鍵盤的示例分析-創(chuàng)新互聯(lián)
文章源于:http://www.dlmjj.cn/article/dpshop.html