单击时,我的编程创建按钮基本上没有被按下。知道为什么吗?没有任何事情发生,动作不会触发,图像也不会改变为替代图像等。
(我正在使用MomentaryChangeButton,但我已将其设置为MomentaryPushIn(以检查它是否正在推动。它并不令人沮丧。)
NSButton *titleButton = [[NSButton alloc]initWithFrame:NSMakeRect(TITLE_BUTTON_X_OFFSET, TITLE_BUTTON_Y_OFFSET, TITLE_BUTTON_WIDTH, TITLE_BUTTON_HEIGHT)];
[titleButton setBordered:NO];
[titleButton setNeedsDisplay:YES];
[titleButton setImage:[NSImage imageNamed:TITLE_BUTTON_NAME]];
[titleButton setAlternateImage:[NSImage imageNamed:TITLE_BUTTON_PRESSED_NAME]];
[titleButton setButtonType:NSMomentaryChangeButton];
[self addSubview:titleButton];
NSButton *aboutButton = [[NSButton alloc]initWithFrame:NSMakeRect(ABOUT_BUTTON_X_OFFSET, ABOUT_BUTTON_Y_OFFSET, ABOUT_BUTTON_WIDTH, ABOUT_BUTTON_HEIGHT)];
[aboutButton setBordered:NO];
[aboutButton setNeedsDisplay:YES];
[aboutButton setButtonType:NSMomentaryChangeButton];
[aboutButton setImage:[NSImage imageNamed:ABOUT_BUTTON_NAME]];
[aboutButton setAlternateImage:[NSImage imageNamed:ABOUT_BUTTON_PRESSED_NAME]];
[[aboutButton image] setSize:NSMakeSize(ABOUT_BUTTON_WIDTH, ABOUT_BUTTON_HEIGHT)];
[[aboutButton alternateImage]setSize:NSMakeSize(ABOUT_BUTTON_WIDTH, ABOUT_BUTTON_HEIGHT)];
[self addSubview:aboutButton];
NSButton *settingsButton = [[NSButton alloc]initWithFrame:NSMakeRect(SETTINGS_BUTTON_X_OFFSET, SETTINGS_BUTTON_Y_OFFSET, SETTINGS_BUTTON_WIDTH, SETTINGS_BUTTON_HEIGHT)];
[settingsButton setBordered:NO];
[settingsButton setNeedsDisplay:YES];
[settingsButton setButtonType:NSMomentaryChangeButton];
[settingsButton setImage:[NSImage imageNamed:SETTINGS_BUTTON_NAME]];
[settingsButton setAlternateImage:[NSImage imageNamed:SETTINGS_BUTTON_PRESSED_NAME]];
[[settingsButton image] setSize:NSMakeSize(SETTINGS_BUTTON_WIDTH, SETTINGS_BUTTON_HEIGHT)];
[self addSubview:settingsButton];
答案 0 :(得分:1)
您必须为每个按钮实施setTarget:
和setAction:
。
完整的工具是:
-(void)awakeFromNib{
NSString *SETTINGS_BUTTON_NAME=@"red.png";
NSString *SETTINGS_BUTTON_PRESSED_NAME=@"green.png";
NSButton *settingsButton = [[NSButton alloc]initWithFrame:NSMakeRect(20, 500, 120, 32)];
[settingsButton setBordered:YES];
[settingsButton setNeedsDisplay:YES];
[settingsButton setButtonType:NSMomentaryChangeButton];
[settingsButton setImage:[NSImage imageNamed:SETTINGS_BUTTON_NAME]];
[settingsButton setAlternateImage:[NSImage imageNamed:SETTINGS_BUTTON_PRESSED_NAME]];
[[settingsButton image] setSize:NSMakeSize(120,32)];
[settingsButton setTitle:@"title"];
[settingsButton setTarget: self];
[settingsButton setAction: @selector(invisible:)]; //invisible is a selector see below
[self.window.contentView addSubview: settingsButton];
}
- (IBAction)invisible:(id)sender{ //change this name accordingly
NSLog(@"yepiiee");
}
编辑:根据我的方便,我做了一些更改,图像和框架矩形,你可以改变你的要求。
答案 1 :(得分:0)
尝试检查要添加按钮的视图的帧。 视图可以绘制其子视图,但如果按钮不在“可见”框架区域,则操作将无法接收。