Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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?

Had trouble with this, as my custom view was deeper in the view hierarchy. Instead, I climbed the responder chain until it finds a UIViewController;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // Pass to top of chain
    UIResponder *responder = self;
    while (responder.nextResponder != nil){
        responder = responder.nextResponder;
        if ([responder isKindOfClass:[UIViewController class]]) {
            // Got ViewController
            break;
        }
    }
    [responder touchesBegan:touches withEvent:event];
}

try adding the method userInteractionEnabled = YES to your UIImageView

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top