Iphone – Push a UIViewController onto UINavigationController from a UIView


Typically, my event handling occurs in the UIViewController, so i used the following line of code:
[self.navigationController pushViewController:viewController animated:YES];

However, now my event handler is in the UIView.
specifically, i'm using - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

So from inside my touchesEnded delegate, how do i push a UIViewController onto a
UINavigationController from a UIView that is a subview of the UIViewController mentioned.

Best Solution

You can make a delegate on your view so that you can refer back to the view's viewController. This could be done like this:

@interface MyCustomView : UIView
    id delegate;

@property(nonatomic, assign) id delegate;

Then in your View controller, you will simply set the view's delegate like this:

[myCustomView setDelegate:self];

Which will allow you to send messages to your delegate from within your custom view

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
    [delegate customMessage];
Related Question