views:

568

answers:

4

In short, I want to detect a touch on the navigation controller titlebar, but having trouble actually catching any touches at all!

Everything is done without IB, if that makes a difference.

My app delegate's .m file contains:

MyViewController *viewController = [[MyViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navigationController.view];

There are a few other subviews added to this window in a way that overlays navigationController leaving only the navigation bar visible.

MyViewController is a subclass of UIViewController and its .m file contains:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
 for (UITouch *touch in touches) {
  NSLog(@"ended\n");
 } 

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 for (UITouch *touch in touches) {
  NSLog(@"began\n");
 } 
}

I also tried putting these functions directly into app delegate's .m file, but the console remains blank.

What am I doing wrong?

+2  A: 

The view controller is inserted into the responder chain between its managed view and the superview:

Because view controllers are tightly bound to the views they manage, they are also part of the responder chain used to handle events. View controllers are themselves descendants of the UIResponder class and are inserted into the responder chain between the managed view and its superview. Thus, if the view managed by a view controller does not handle an event, it passes the event to its view controller, which then has the option of handling the event or forwarding it to the view’s superview.

(the UIViewController documentation)

Is it possible, that the managed view of your controller is eating all the events? What kind of view is it?

Dirk
I am fairly new to this and not fluent with the whole view/controller relation, but in my case, the view inside MyViewController is actually blank. I just tried making a custom UIView subclass and overriding the touches methods from there, then using that subclass for the view of this controller, but that did not work either. The only purpose of this MyViewController is to provide a navigation bar with buttons and title.
SaltyNuts
A: 

Well, for lack of a better idea, I added another subview to my app, clear in color, placed programmatically over the navigation bar title and used a custom class for that view with relevant touch methods overridden. Works fine, but the I still wish there was a more elegant solution.

SaltyNuts
u shoudl edit your post instead of posting an answer
Daniel
A: 

These methods should be put into the UIView subclass not the UIViewControllers...The UIView will receive the touches call backs, then you can make a protocol on the UIView and implement it on the UIViewController so the UIViewController will receive some call back when the touch events occur...Here is a link that talks about protocols and how to define them and implement them Protocols Ref

Daniel
A: 

Asalam o alkum, Can any buddy give me full detail of touch event function used in opengl xcode..I am facing some problem in working with this function,

location = [touch locationInView:self];

In the above line when I print the value of location in the console form .. then it give me this value 1119354880 and when I print the bounds.size.height value it give me the the value 1081495552...

I want to handle these two value ,, when the user touches it will begin painting and the end value is 500.

If any buddy have some answer regarding this then send it to my email id..

[email protected] or [email protected]

JazakAllah

&

thnx.