Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

So I subclassed UIViewController and in the nib I have a UIView, and in it a tableview. It is my understanding that both UIViewController and UIView are subclasses of UIResponder, so they should receive the - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method.

However, this is not the case, and my view controller subclass is not receiving that method. I'd really like to not subclass the UIView, if that's allright.

I am trying to implement this , but my

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    [textfield resignFirstResponder];

Is not getting called.

It depends on where you touch. If you touch in the table view the touches are sent to the tableview, not the view controller. If you touch in a textfield that is in your view, the touches are sent to the UITextField itself. If you touch outside the tableview, outside the textfield but in the viewcontrollers' view then the view controller should get those touches.

Not without subclassing a UIView--which the poster doesn't want to. If you change it to a UIControl you can access the events, so you can set it to a touchDown or touchUpInside in IB without having to muck about it code. I've done this before. A UIView doesn't list touch events. – MishieMoo Sep 1, 2010 at 22:34

Unfortunately, touchesBegan only takes place in the view, despite UIViewController being a subclass of UIReponder. The docs for touchesBegan say this.

Tells the receiver when one or more fingers touch down in a view or window.

touchesBegan is also supported in a UIViewController. I use it extensively in one of my apps to allow the user to rearrange the views in the viewcontroller. – progrmr Sep 1, 2010 at 22:39 UIViewController does participate in the responder chain and will therefore receive touchesBegan just like a UIView would. – Boon Feb 2, 2012 at 5:38 developer.apple.com/library/ios/#DOCUMENTATION/EventHandling/… The hit-test view or first responder passes the event or message to its view controller if it has one; if the view doesn’t have a view controller, it passes the event or message to its superview. If a view or its view controller cannot handle the event or message, it passes it to the superview of the view. – bbrame Feb 29, 2012 at 17:45 I'm glad to see so many people have down voted my terrible answer. I was clearly working from some extremely outdated knowledge there. – Jerry Jones Jun 26, 2012 at 17:39

Is the UIView you are talking about is one you have created, or are you using the one furnished with the UIViewController ? Further more, if the click happen on the UITableView, you will have to subclass this to let your UIViewController know that a click occurred on the tableview...

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.