views:

424

answers:

4

Hey all,

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 http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/32204-moving-uitextfield-when-keyboard-pops-up.html but my

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

Is not getting called.

A: 

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.

Jerry Jones
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
A: 

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...

TheSquad
A: 

If you change your VC's view to a UIControl (UIControl is the superclass of a UIView) in IB, you can assign it an IBAction. You can have your textfield resignFirstResponder in that action.

MishieMoo
A UIView *is* a UIControl, no need to change anything. You can assign actions to UIViews.
progrmr
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
+1  A: 

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.

progrmr