gpt4 book ai didi

Objective-c:NSButton setAction 不工作

转载 作者:太空狗 更新时间:2023-10-30 03:25:05 28 4
gpt4 key购买 nike

我正在以编程方式制作几个按钮并尝试为每个按钮设置一个 Action ,但我似乎无法让它工作。

在我的 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 不会被击中...

最佳答案

最可能的原因是 responder chain for action messages 中没有对象回应这些行动。当您指定一个 Action 但未指定目标时,Cocoa 会尝试通过遍历响应者链来找到响应这些 Action 消息的对象。

要么确保您在响应者链中有合适的对象,要么如果您有对该对象的引用,请将其指定为这些操作的目标:

[btnZoomIn setAction:@selector(zoomIn:)];
[btnZoomIn setTarget:objectThatRespondsToZoomIn];

[btnZoomOut setAction:@selector(zoomOut:)];
[btnZoomOut setTarget:objectThatRespondsToZoomOut];

关于Objective-c:NSButton setAction 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7365625/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com