2010-06-15 56 views
5

我嘗試了很多東西,仍然沒有結果。UIButton addTarget:action:forControlEvents:results in [NSObject doesNotRecognizeSelector:]

所以我在UIViewController中的一個子類編程創建了以下按鈕:

rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0); 
    rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0]; 
    [rightButton setTitle:@"MyTitle" forState:UIControlStateNormal]; 
    rightButton.backgroundColor = [UIColor clearColor]; 
    [rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; 
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 
    [rightButton setBackgroundImage:normalImage forState:UIControlStateNormal]; 
    [rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted]; 
    [rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:rightButton]; 

其中,選擇是:

- (void)myButton; 

我什麼都試過:

- (void)myButton; 
- (void)myButton:(id)sender; 
- (void)myButton:(id)sender forEvent:(UIEvent *)event; 
- (IBAction)myButton; 
- (IBAction)myButton:(id)sender; 
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event; 

和當然相應的選擇器:

[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside]; 
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside]; 
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside]; 
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside]; 
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside]; 
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside]; 

結果始終是未捕獲的異常 - [NSObject doesNotRecognizeSelector:]。然而,該計劃通常的回溯是:

#0 0x92a6bedb in objc_msgSend() 
#1 0x03b0a430 in ??() 
#2 0x00306b4e in -[UIControl sendAction:to:forEvent:]() 
#3 0x00308d6f in -[UIControl(Internal) _sendActionsForEvents:withEvent:]() 
#4 0x00307abb in -[UIControl touchesEnded:withEvent:]() 
#5 0x002bcddf in -[UIWindow _sendTouchesForEvent:]() 
#6 0x002a67c8 in -[UIApplication sendEvent:]() 
#7 0x002ad061 in _UIApplicationHandleEvent() 
#8 0x02498d59 in PurpleEventCallback() 
#9 0x01cabb80 in CFRunLoopRunSpecific() 
#10 0x01caac48 in CFRunLoopRunInMode() 
#11 0x02497615 in GSEventRunModal() 
#12 0x024976da in GSEventRun() 
#13 0x002adfaf in UIApplicationMain() 

那麼這個按鈕有什麼問題?

PS:我使用的是iPhone SDK 3.1.3


更新!

在AppDelegate中下面的代碼(在界面上沒有聲明):

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)]; 
    [test setTitle:@"Title" forState:UIControlStateNormal]; 
    UIImage *bg = [UIImage imageNamed:...]; 
    [test setBackgroundImage:bg forState:UIControlStateNormal]; 
    [test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside]; 
    [window addSubview:test]; 
    [test release]; 
    [window makeKeyAndVisible]; 
} 

- (void)testAction { 
    NSLog(@"Write something..."); 
} 

作品完美!

但是,如果我創建一個具有相同的代碼一個空的UIViewController:

- (void)viewDidLoad { 
    UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)]; 
    [test setTitle:@"Title" forState:UIControlStateNormal]; 
    UIImage *bg = [UIImage imageNamed:...]; 
    [test setBackgroundImage:bg forState:UIControlStateNormal]; 
    [test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:test]; 
    [test release]; 
    [window makeKeyAndVisible]; 
} 

- (void)testAction { 
    NSLog(@"Write something..."); 
} 

我得到這個神祕的錯誤。 :-(幫助

回答

8

解決方案!

我的問題是,我用在AppDelegate中下面的代碼:

UIViewController *controller = [[MyCustomController alloc] init]; 
[window addSubview:controller.view]; 
[controller release]; 

因此,只有視圖控制器的視圖屬性被保留和其他一切都被清除。視圖控制器沒有任何功能(沒有觸摸事件,沒有實例方法)。這就是爲什麼addTarget:action:forEvents:不起作用的原因。

因此,做到這一點 - 在AppDelegate的dealloc中釋放視圖控制器。;)

+0

ARC引入後,解決方案稍有不同。現在你需要在頭文件中聲明UIViewController *控制器。所以它是類的一個成員變量,它在內部保留。 就像你知道的那樣,不要把它叫做ARC。 – 2014-03-13 07:16:10

0

哪來則myButton被宣佈 如果是合成的,然後代替

rightButton = [UIButton的 buttonWithType:UIButtonTypeCustom]!?

使用

self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

應該工作。如果沒有,請張貼你如何宣佈財產。

+0

我不認爲這是這裏的問題。這個視圖控制器以編程方式創建它的視圖(根本沒有xib文件)。該按鈕只是該類中的一個實例變量(不是屬性),因此: self.rightButton = ... 根本不編譯。 我甚至在代碼中創建了一個按鈕。 UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom]; ... [self.view addSubview:button2]; 當我沒有添加目標和動作時,按鈕呈現並正常動作。當我添加功能(addTarget:action:forEvents :)時,點擊會引發異常。 – Teodor 2010-06-15 16:28:38

31

舊的文章,可能所有的現在進行排序,但還有另外一個問題,這將導致崩潰:

[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside] 

不正確。 action:@selector(testAction)必須有一個冒號有這樣的:

action:@selector(testAction:) 

,否則你會得到一個無法識別的選擇崩潰 - 這是你做了什麼..

+1

我希望我可以爲此投票+100,你只是爲我節省了幾個小時的頭疼。如果你錯過冒號設置沒有Id的選擇器目標,但不是很有用。添加冒號會發揮魔力。 很少有我看到代碼如此晦澀難懂並且很容易被忽略 – Cruachan 2011-06-27 19:14:19

+1

是的 - 剛剛碰到過這個自己---這個冒號很重要---謝天謝地skajam66寫了這個回覆。 – David 2011-08-16 19:22:37

+0

有同樣的問題。感謝您指出這一點! – 2012-06-06 13:01:01