2009-07-08 86 views
1

通常,我的事件處理髮生在UIViewController中,所以我用下面的代碼行: [self.navigationController pushViewController:viewController animated:YES];從UIView將UIViewController推到UINavigationController上?

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

所以從我的touchesEnded委託,我怎麼推一個UIViewController到 的UINavigationController從一個UIView那是的UIViewController的一個子視圖裏面提到。

回答

2

您可以在視圖上創建一個委託,以便您可以引用回視圖的viewController。這可能就像這樣:

@interface MyCustomView : UIView 
{ 
    id delegate; 
} 

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

然後,你可以簡單的設置視圖的委託是這樣的:

[myCustomView setDelegate:self]; 

,讓您從內消息發送到您委託你定製視圖

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

您需要將對您的UIViewController的引用添加到您的UIView。然後在事件處理程序中,發送消息給UIViewController,這將推送另一個控制器。您也可以在事件處理程序中進行推送,但另一種方式在MVC體系結構中更「正確」。

相關問題