CrazySoftware
CrazySoftware

Reputation: 133

Make keyboard disappear with textField and textView

I'd like to dismiss the keyboard with a text field using - (BOOL)textFieldShouldReturn:(UITextField *)textField {, but I need to do the same with a text view, so I'll use - (BOOL)textViewShouldReturn:(UITextView *)textView {. Is it possible to put them togheter in the same AppDelegate?

Thank you.

Upvotes: 1

Views: 1183

Answers (5)

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

Yes, you can have two delegates

@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>

Unfortunately

The protocol UITextViewDelegate does not have, something like this.

- (BOOL)textViewShouldReturn:(UITextView *)textView {


UITextViewDelegate Protocol, enter image description here

EDIT 1 :


Button press event to hide the keyboard.

-(IBAction) yourButtonPressed:(id)sender;{

    for(UIView *v in self.view.subviews){
        if([v isKindOfClass:[UITextField class]] || [v isKindOfClass:[UITextView class]]){
            if([v isFirstResponder]){
                [v resignFirstResponder];
                break;
            }
        }
    }

}

Upvotes: 1

Bharathi D
Bharathi D

Reputation: 1013

Yes, you can use textfield delegate and textview delegate in the same application.

TextField:

@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>

// This allocates the textfield and sets its frame (or) you can use interfaceBuild
   UITextField *textField = [[UITextField  alloc] initWithFrame:
   CGRectMake(20, 50, 280, 30)]; 

  textField.delegate=self;

// This method enables or disables the processing of return key
-(BOOL) textFieldShouldReturn:(UITextField *)textField{    
    [textField resignFirstResponder]; // this is event for hide keyboard.
    return YES;
}

TextView:

// init
 UITextView *textView = [[UITextView  alloc] initWithFrame:
       CGRectMake(20, 50, 280, 30)]; 

 - (void)textViewDidChangeSelection:(UITextView *)textView
{
 [textField resignFirstResponder]; // this is event for hide keyboard.
}

Upvotes: 0

matt
matt

Reputation: 535945

Yes, with a UITextView it's tricky. As I say in my book...

http://www.apeth.com/iOSBook/ch23.html#_uitextview

...on the iPad, the problem of dismissing the keyboard doesn't arise because the user can dismiss it with the button in the lower right corner of the keyboard. So this leaves only the iPhone. You will typically have an interface such that there is a Done button or similar. Look at how the Notes app solves this, for example.

The process itself is just the same: call endEditing: on the superview and whoever is first responder will cease being first responder and the keyboard will retire.

Upvotes: 0

Mark McCorkle
Mark McCorkle

Reputation: 9414

Just subscribe to the delegates from your viewController and make sure to set the delegate on those objects to your viewController.

.h

@interface YourViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>

.m

someTextField.delegate = self;
someTextView.delegate = self;

From the sound of it you just need to tie into the actions of the textField and textView. Create an IBAction and tie it to what you'd like. Then you can resignFirstResponder from that IBAction.

Use this action for both your textField and textView

- (IBAction)lowerTheText:(id)sender 
{
    [sender resignFirstResponder];
}

Upvotes: 0

d4Rk
d4Rk

Reputation: 6693

I don't fully understand your question... Basically you will do the following:

  • implement the UITextFieldDelegate and UITextViewDelegate in your viewController
  • implement the methods of that delegates, you need/want

  • resignFirstResponder / endEditing of the textField / textView, where ever you want

Upvotes: 0

Related Questions