NSButton不能setaction

时间:2018-04-15 13:02:33

标签: objective-c ibaction nsbutton

我正在尝试向NSButton添加操作,但它无效

我以编程方式创建NSButtton

-(NSView *)rowTable:(CGFloat) heightOnPanel :(NSString *) textValue: (CGFloat) width: (CGFloat) height :(int) intColor{
    NSView *viewce = [[NSView alloc] initWithFrame:CGRectMake(0, heightOnPanel, 320, 40)];
    NSButton *layerBt = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, width, height)];

    [layerBt setTitle:textValue];

    [layerBt setButtonType:NSMomentaryPushInButton];
    [layerBt setTarget:nil];
    [layerBt setAction:@selector(buttonPressed:)];
    [viewce addSubview:layerBt];

    return viewce;
}

这里是IBAction方法:

- (IBAction)buttonPressed:(id) sender{
    NSLog(@"hello world");
}
你能指出我错过的东西吗?

由于

1 个答案:

答案 0 :(得分:0)

如果方法buttonPressed:的对象是响应者链的成员,则代码应该有效。如果不是,您应该明确设置目标:

[layerBt setTarget:theTarget];
[layerBt setAction:@selector(buttonPressed:)];

其中theTarget实现buttonPressed: