IOS中開發(fā)UITextView回收或關(guān)閉鍵盤的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹IOS中開發(fā)UITextView回收或關(guān)閉鍵盤的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

從事光華機(jī)房服務(wù)器托管,服務(wù)器租用,云主機(jī),網(wǎng)頁空間,國際域名空間,CDN,網(wǎng)絡(luò)代維等服務(wù)。

IOS 開發(fā)UITextView回收或關(guān)閉鍵盤

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]; 
}

以上是“IOS中開發(fā)UITextView回收或關(guān)閉鍵盤的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前文章:IOS中開發(fā)UITextView回收或關(guān)閉鍵盤的示例分析-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article8/dpshop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、微信小程序、網(wǎng)站維護(hù)、品牌網(wǎng)站設(shè)計(jì)、微信公眾號、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)