从UIView将UIViewController推到UINavigationController上?

问题描述:

通常,我的事件处理发生在UIViewController中,所以我用下面的代码行: [self.navigationController pushViewController:viewController animated:YES];从UIView将UIViewController推到UINavigationController上?

然而,现在我的事件处理程序是在UIView的。 具体而言,我使用- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

所以从我的touchesEnded委托,我怎么推一个UIViewController到 的UINavigationController从一个UIView那是的UIViewController的一个子视图里面提到。

您可以在视图上创建一个委托,以便您可以引用回视图的viewController。这可能就像这样:

@interface MyCustomView : UIView 
{ 
    id delegate; 
} 

@property(nonatomic, assign) id delegate; 
在您的视图控制器

然后,你可以简单的设置视图的委托是这样的:

[myCustomView setDelegate:self]; 

,让您从内消息发送到您委托你定制视图

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

您需要将对您的UIViewController的引用添加到您的UIView。然后在事件处理程序中,发送消息给UIViewController,这将推送另一个控制器。您也可以在事件处理程序中进行推送,但另一种方式在MVC体系结构中更“正确”。