2010-07-22 74 views
0

我正在研究一個基本的iPhone應用程序來測試一些事件,並且我遇到了一個我無法理解或找到任何答案的錯誤。我根本沒有使用IB(除了它創建的MainWindow.xib)。iPhone - 無法識別的選擇器,「可能不會響應addTarget」

現在,它是儘可能基本的。

mainAppDelegate.h

#import <UIKit/UIKit.h> 
#import "mainViewController.h" 

@interface mainAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    mainViewController *viewController; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) mainViewController *viewController; 

@end 

mainAppDelegate.m

#import "mainAppDelegate.h" 

@implementation mainAppDelegate 

@synthesize window; 
@synthesize viewController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    self.viewController = [[mainViewController alloc] init]; 
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

- (void)dealloc { 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 

@end 

mainViewController.h

#import <UIKit/UIKit.h> 

@interface mainViewController : UIViewController { 

} 

- (void)showMenu; 

@end 

mainViewController.m

#import "mainViewController.h" 

@implementation mainViewController 

- (void)loadView { 
    UIScrollView *mainView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    mainView.scrollEnabled = YES; 
    self.view = mainView;  
    self.view.backgroundColor = [UIColor grayColor]; 

    [self.view setUserInteractionEnabled:YES]; 
    [self.view addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchDown]; 

    [mainView release]; 
} 

- (void)showMenu { 
    NSLog(@"Show Menu"); 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 

現在,我得到一個警告,在這條線:

[self.view addTarget:self action:@selector(showMenu) forControlEvents:UIControlEventTouchDown]; 

,說-addTarget「的UIView可能不響應 ':動作:forControlEvents:'。這是沒有意義的,因爲UIView子類當然可以響應addTarget,並且我在self.view上調用它,它必須存在,因爲直到loadView結束才釋放它。 (甚至那麼它應該可以由控制器保留)

尋找以跟蹤顯示實際的錯誤是 - [UIScrollView中addTarget:動作:forControlEvents:]:無法識別的選擇發送到實例0x5f11490

所以它看起來這是選擇器本身的問題,但我看到我的選擇器沒有錯!

我對此很困惑,任何幫助都會很棒。

回答

3

首先,類總是以大寫字母開始....

UIScrollViewUIView一個子類,而不是UIControl

UIControl implements addTarget:action:forControlEvents:UIScrollView沒有。因此,運行時錯誤。

如果您希望響應在滾動視圖上採取的操作而發生某些情況,請爲滾動視圖設置委託。見UIScrollViewDelegate's documentation

+0

「UIControl」的+1。作爲進一步的見解,'UIControl'就是你所有的'UIControlEvents',例如觸摸或修改。如果對象不是'UIControl'的子類,那麼你就不能在它上面使用這些事件。一個簡單的例子就是打開一個包含視圖的XIB。點擊視圖,進入檢查器窗口,你會看到它沒有任何控制事件。現在,如果轉到inspector窗口的最後一個選項卡並將其設置爲「UIControl」子類而不是「UIView」,那麼它神奇地包含了所有可以連接的事件。 – iwasrobbed 2010-07-22 16:10:11

+0

謝謝,這正是我需要知道的。我應該知道檢查。 當你說'班總是以大寫字母開頭' - 那是公約還是必要的? – 2010-07-22 16:31:25

+1

Objective-C公約,儘管我們許多人認爲有必要避免引起混亂的瘋狂。 :) – bbum 2010-07-22 16:42:35

0

嘗試這種微妙的變化

[self.view addTarget:self action:@selector(showMenu:) 
           forControlEvents:UIControlEventTouchDown]; 

- (void)showMenu:(id) sender { 
    NSLog(@"Show Menu"); 
} 
+0

謝謝Aaron,解決了我的問題。我正在關注官方的Facebook SDK文檔,冒號失蹤。 – andyc 2011-12-30 13:06:57

相關問題