我正在編程一些按鈕,並試圖爲每個按鈕設置一個動作,但我似乎無法使其工作。Objective-c:NSButton setAction不起作用
在我AppController.h我有這樣的代碼:
...
IBOutlet NSButton* btnZoomIn;
IBOutlet NSButton* btnZoomOut;
...
和
- (IBAction) zoomIn : (id) sender;
- (IBAction) zoomOut : (id) sender;
和AppController.m的awakeFromNib方法中:
/*zoom in and out buttons*/
//get the path to the image files
NSString* zoomInImgPath = [[NSBundle mainBundle] pathForResource:@"zoomIn" ofType:@"png"];
NSString* zoomOutImgPath = [[NSBundle mainBundle] pathForResource:@"zoomOut" ofType:@"png"];
//declare the NSImages
zoomInImg = [[NSImage alloc] initWithContentsOfFile:zoomInImgPath];
zoomOutImg = [[NSImage alloc] initWithContentsOfFile: zoomOutImgPath];
//button making!
//zoomIn
btnZoomIn = [[NSButton alloc] initWithFrame:NSMakeRect(1426.0, 920.0, 25.0, 25.0)];
[btnZoomIn setButtonType:NSMomentaryPushInButton];
[btnZoomIn setTitle:@""];
[btnZoomIn setToolTip:@"Zoom In"];
[btnZoomIn setImage:zoomInImg];
[btnZoomIn setAction:@selector(zoomIn:)];
[[mainWin contentView] addSubview:btnZoomIn];
//zoomOut
btnZoomOut = [[NSButton alloc] initWithFrame:NSMakeRect(1456.0, 920.0, 25.0, 25.0)];
[btnZoomOut setButtonType:NSMomentaryPushInButton];
[btnZoomOut setTitle:@""];
[btnZoomOut setToolTip:@"Zoom Out"];
[btnZoomOut setImage:zoomOutImg];
[btnZoomOut setAction:@selector(zoomOut:)];
[[mainWin contentView] addSubview:btnZoomOut];
和
- (IBAction) zoomIn : (id) sender {
NSLog(@"zoom in!");
}
- (IBAction) zoomOut : (id) sender {
NSLog(@"zoom out!");
}
但縮小(ZoomOut)和zoomIn不要撞到......
嘗試設置了'target'爲好。 – PengOne
如果您不使用ARC或GC,請不要忘記釋放或自動釋放圖像和按鈕。 –