2013-10-08 10 views
1

我正在創建UIGestureRecognizer並將其設置爲父目標UIViewController。 Xcode警告我動作選擇器未聲明。UIGestureRecognizer initWithTarget設置爲類id(UIViewController)的對象會導致編譯器警告未聲明的操作選擇器

UITapGestureRecognizer *tapTest = [[UITapGestureRecognizer alloc] initWithTarget:self.traverseResponderChainforUIViewController action:@selector(handleTap:)]; 

Warning: Undeclared selector 'handleTap:' 

這可能也是值得記錄的是我的traverseResponderChainforUIViewController方法返回ID(儘管實現最終應返回的東西isKindOfClass:[UIViewController class] or nil):

- (id)traverseResponderChainforUIViewController; 

最後......這工作完全在運行時,因爲選擇是實際上在對象UIViewController中聲明瞭對象。

- (void)handleTap:(UIGestureRecognizer *)recognizer 
{ 
    NSLog(@"%@", recognizer.description); 
} 

怎樣才能讓編譯器在這裏開心?

我不想改變我的程序架構(動作從視圖發送到視圖控制器),但我做錯了(TM)或我只是錯過了一個完全適當的步驟實施?

回答

0

試試這個

YourViewControllerClass *vc = (YourViewControllerClass *)self.traverseResponderChainforUIViewController; 
UITapGestureRecognizer *tapTest = [[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(handleTap:)]; 
+0

鑄字....我真的以爲這會工作....但事實並非如此。 –

+2

這可能是一個愚蠢的問題,但是在ViewController的公共接口中定義了handleTap? –

+0

那種......作品。它仍然感覺有點不舒服(儘管我是主觀的) - 你仍然需要將UIViewController頭文件導入到UIView中,UIVestureRecognizer被初始化,編譯器甚至在公共接口中識別該方法。 我可能會嘗試在稍後使用協議來解決此問題(很確定我可以通過創建一個實現帶有handleTap方法的協議的id對象來實現此目的),但現在已經過於複雜了。 –

相關問題