UITextView hide keyboard in iphone

谁都会走 提交于 2019-11-29 13:28:17

问题


I want to hide keyboard when a user presses return in UITextView object in iphone. However, mysteriously this is not working for UITextView but working for UITextField. I am unable to figure out why...

This is what I did:

1) I created a view based application in XCode4.

2) in .xib created UITextView, UITextField and UIButton objects

3) Marked both UITextField and UITextView delegates to File's Owner in Outlets

4) Added <UITextFieldDelegate> to @interface UIViewController in .h

5) Added textFieldShouldReturn function in .m

Here are the codes:

.h file

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{

    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end

.m file

- (BOOL) textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"textFieldShouldReturn Fired :)");
    [textBoxLarge resignFirstResponder];
    [textBoxLittle resignFirstResponder];
    return YES;
}

Amazingly, the keyboard is disappearing in case of textBoxLittle (UITextField) but not in case of textBoxLarge(UITextView)

As a further check I, made the button to call function doSomething

- (IBAction)doSomething:(id)sender {
    [textBoxLarge resignFirstResponder];
    [textBoxLittle resignFirstResponder];
}

When I am pressing the button, keyboard is disappearing in both textboxes.

Its driving me nuts why textFieldShouldReturn is working for small textbox, but NOT for large textbox.

Please Help!


回答1:


You need to write code in UITextViewDelegate and assign it to your class.




回答2:


Three things:

Make your view implement UITextViewDelegate.

@interface keyboardDisappearViewController : UIViewController
    <UITextFieldDelegate, UITextViewDelegate>

Add the following method:

- (BOOL)textView:(UITextView *)textView
        shouldChangeTextInRange:(NSRange)range
        replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
    }
    return YES;
}

Set the file's owner as delegate for the UITextView in the interface builder.

(BTW: Solution copied from the comments to the previous answer, as it took me a while to extract. I though others could benefit from my experience.)




回答3:


Simple trick Set delegate for your text view and then

doSomething 
{

}

action connect to ext view for control event didEndOnExit and tuchupinside


// To dismiss key board when user clicks enter/return

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{
    if([text isEqualToString:@"\n"]) 
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}



回答4:


Use this code

Inherit UITextViewDelegate protocol in your Viewcontroller add the text

@interface YourViewController () <UITextViewDelegate>

In viewDidLoad set yourself as a delegate:

yourUITextView.delegate = self;

Implement the delegate method below:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{ 
   return NO; 
}



回答5:


Key Points

  • Someone needs to be listening for the Return Key press

    • UITextFieldDelegate
    • textFieldShouldReturn(textField : UITextField)
  • Someone needs to manually dismiss the keyboard

    • resignFirstResponder()

#1: Create a UITextFieldDelegate and assign it as the delegate to your UITextField -

exampleTextField.delegate = yourUITextFieldDelegate;

#2: Have 'yourUITextFieldDelegate' contain the following -

func textFieldShouldReturn(textField : UITextField) -> Bool {

    self.titleField.resignFirstResponder();   //Here's the key!!!!!

    return true;                              //true just says 'default behavior'
} 



回答6:


If you came here looking for the Swift solution, like I did, here you are :)

extension keyboardDisappearViewController : UITextViewDelegate {
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        textView.resignFirstResponder()
    }
    return true
}}



回答7:


You have coded in the .h file:

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextView *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end

It should be:

@interface keyboardDisappearViewController : UIViewController <UITextFieldDelegate>
{
    UITextView *textBoxLarge;
    UITextField *textBoxLittle;
}
@property (nonatomic, retain) IBOutlet UITextField *textBoxLarge;
@property (nonatomic, retain) IBOutlet UITextField *textBoxLittle;

- (IBAction)doSomething:(id)sender;
@end


来源:https://stackoverflow.com/questions/6680693/uitextview-hide-keyboard-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!