質問

I have a static UITableView that contains a number of UITextFields. When the user taps outside of any of the text fields I'd like to I'd like to dismiss the keyboard.

At the top level of my UIViewController is a UITableView and a UITabBarItem. I believe I'll also have to handle taps on the status bar.

I am unsure as how I should register for touches on them (so that I can force any of the text fields to call resignFirstResponder.

I thought I might have to handle UIResponder's

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

for the table view and tab bar item but neither of them are UIControls so I can't use

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

UIWindow is also a UIResponder but again, I can't seem to get touch events for it.

Any help would be much appreciated, CS

役に立ちましたか?

解決 2

When you have a static UITableView it "eats" the touch events. So what I did was by subclassing UITableView and then added a delegate which reports touch events.

他のヒント

If you had one text field I'd add touches began into the UIViewController it belongs to and do it like this...

- (void)touchesBegan ... cant remember full name
{
    if ([touches count] > 1) {
        return;
    }

    UITouch *touch = [touches anyObject];

    CGPoint touchPoint = [touch locationInView self.view];

    if (!CGRectContainsPoint(self.textField.frame, touchPoint)) {
        //touch originated outside textField.

        [textField resignFirstResponder];
    }
}

If you have more than one text field then just do the CGRectContainsPoint check for each of them inside the if.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top