Iphone Textview does not invoke TouchesBegan

∥☆過路亽.° 提交于 2019-12-23 20:07:25

问题


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?


回答1:


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.




回答2:


The best method I've seen for this is to add a transparent subview that overlays the text view and handles the TouchesBegan first. You can then detect touches outside the text field and dismiss the keyboard by having the text field resign as first responder.

For example, create the overlay subview in IB or programmatically, either way. Place and size it so that it covers the text view(s), and give it a clear color. If you add the view via IB, hide it when your main view loads so that it doesn't absorb touches just yet, like so:

- (void)viewDidLoad
{
    [super viewDidLoad];
    overView.hidden = YES;
}

Then when the text field begins editing, unhide the view:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    overView.hidden = NO;
}

When the text field ends editing, rehide the view:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    overView.hidden = YES;
}

Add a touchesBegan to detect when your unhidden overView is touched:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    // Resign first responder if touch is outside text field
    if ([touch view] != myTextField)
        [myTextField resignFirstResponder];

    // Send touches up the responder chain (pass them through)
    [self.nextResponder touchesBegan:touches withEvent:event];
}

You can also do this through custom gestures, but that only works with iOS 4.x and, in my opinion, is more complicated.




回答3:


I'm assuming your UITextView is not editable as you don't want the keyboard to pop up when you touch it, so then make sure in the View properties for your UITextView that "User Interaction Enabled" is not checked. You can also write this in your ViewController as such:

textView.userInteractionEnabled = NO;

This will allow user events to pass through to the superview, and touchesBegan and those other delegate methods will be invoked.



来源:https://stackoverflow.com/questions/4198798/iphone-textview-does-not-invoke-touchesbegan

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