Search code examples
iphoneuitextfielduitextviewtouchesbegan

Iphone Textview does not invoke TouchesBegan


I have a Textfield which hides the keyboard when I touch somewhere else on the screen (through my TouchesBegan function and resign...blah). But when I touch a Textview the TouchesBegan does not get invoked and the keyboard doesn't hide! Is there any way to invoke TouchesBegan in order to hide the keyboard?


Solution

  • There is also a easy way to hide the keyboard on touchesBegan: when you are using UITextView to enter the data.

    Step 1. Be sure that you have extended Textviewdelegate while class declaration.

    @interface YourClassName : UIViewController { IBOutlet UITextView *txtMyView; }

    step 2. set the delegate to self in view did load function.

    - (void)viewDidLoad
    {
        txtMyView.delegate = self;
    }
    

    step 3. write the similer code in touchesbegan function with you textView name.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
                [txtMyView resignFirstResponder];
            [super touchesBegan:touches withEvent:event];
    
    }
    

    Thanks and regards.