UIButton addTarget:action:forControlEvents:[NSObject doesNotRecognizeSelector:]中的结果

时间:2010-06-15 14:53:12

标签: iphone uibutton uicontrol uncaught-exception

我尝试了很多东西,但仍然没有结果。

所以我在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...");
}

我得到了这个神秘的错误。 :-(帮助!

3 个答案:

答案 0 :(得分:31)

一篇旧帖子,现在可能全部排序,但还有另一个会导致崩溃的问题:

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

不正确。因此action:@selector(testAction)必须有一个冒号:

action:@selector(testAction:)

否则你会得到一个无法识别的选择器崩溃 - 这就是你得到的......

答案 1 :(得分:8)

解决方案!

我的问题是我在AppDelegate中使用了以下代码:

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

因此,只保留了视图控制器的视图属性,并清除了其他所有内容。视图控制器没有任何功能(没有触摸事件,没有实例方法)。这就是addTarget:action:forEvents:无效的原因。

所以,聪明一点 - 在AppDelegate的dealloc中释放你的视图控制器。 ;)

答案 2 :(得分:0)

我的myButton被宣布在哪里? 如果它是合成的,那么而不是

  

rightButton = [UIButton   buttonWithType:UIButtonTypeCustom];

使用

self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];

应该有用。如果没有,请发布您如何宣布该物业。