You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How does UIView pass Touch Events to UIViewController?

How does UIView pass Touch Events to UIViewController?

What am I missing?

iPhone, Other OS

Posted on May 1, 2008 9:55 AM

Reply
10 replies

May 1, 2008 12:34 PM in response to rizwansattar04

Say you have UIView called 'TouchView1' where u are going to track the touches.
In TouchView1 class you have the instance variable of the corresponding controller type. TouchViewController is your UIViewController.

TouchViewController *touchController;

your TouchView1 class should have either touchesMoved or touchesBegan delegate methods.

in the TouchViewController, create instance variable of type TouchView1. In the loadview method, instantiate the TouchView1 and add to your container view if you have. And set the
TouchView1.touchController =self;
Also you have the method which should be called when a touch occurs on the view. (say TouchMethod ) .


Now when you touch the TouchView1, touchesMoved and touchesBegan (correct usage of these methods depends on you) will be called and inside these method, call the function 'TouchMethod' which is in the TouchViewController .
[touchController TouchMethod];

hope it helps you...

Message was edited by: subbump

May 3, 2008 4:44 AM in response to JungleFighterPhred

Hi,

I'm trying to integrate the method described above but I'm missing something crucial.

I have this code in my uiviewcontroller class:

- (void)loadView {

CGRect frame;

// add the top-most parent view
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
contentView.touchController =self;
**error: request for member 'touchController' in something not a structure or union

[contentView release];

}

- (void)touchesChangedWithEvent:(UIEvent*)event
{
NSSet *touches = [event allTouches];
for (UITouch *myTouch in touches)
{
if (myTouch.phase == UITouchPhaseBegan) {
// new touch handler
[self flipAction];
}
}

May 3, 2008 2:23 PM in response to Superzhou

Hi,

After some trial and error I can get the touch event to trigger. But it can only call methods in the appdelegate class. If I try to call a method in a different file I get this runtime error:

unrecognized selector sent to class

I also get this warning when compiling:

warning: myview may not respond to mymethod
messages without a matching message signature will be assumed to return "id" and accept "..." as arguments

May 19, 2008 12:21 AM in response to kotau

The above example worked as a receiver but not as a complete solution as I was having problems getting the function called by the UIView subclass (TapView) to work properly including having issues with removeFromSuperView...

Maybe this will be helpful for saving someone else some time. Hopefully I don't get sued for breaching the NDA 🙂

In TapView.h I had to add:

@class TouchView1;

@interface TapView : UIView
{
TouchView1 * touchController;
}

@property (nonatomic, retain) TouchView1 * touchController;


in the TapView.m source file:

@synthesize touchController;


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[touchController TouchFunction];
}

in TouchView.h:

@class ImageTapView;

@interface TouchView1 : UIViewController {
TapView *contentView;

}

@property (nonatomic, retain) TapView *tapView;

@end

Then in the touchView1.m source file:

@synthesize tapView;

loadView{

contentView = [[ImageTapView alloc] initWithFrame:frame;
self.view = contentView;
contentView.touchController = self;

}


-(void) TouchFunction{

do something here...


}



Cheers.

Jun 17, 2008 9:42 AM in response to kotau1

I am having a related issue:
In my applicaton I want to subclass UITableViewController to create a tableview and there is a picture in one Cell, I am trying catch touch events in that cell, like move right to display next picture and move left to display previous picture.
I subclassed UITableViewCell( subclass of UIView) to create CustomCell, but I could not get the touch event. Did the CustomCell's superview get the events and not dispatch to the CustomCell?
How can I implement this? Any suggestion are highly appreciated, Thanks a lot.

How does UIView pass Touch Events to UIViewController?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.