macos 发送一个动作可可 - IBAction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/833658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send An Action Cocoa - IBAction
提问by Ben Reeves
I'd like to send an action to another object using cocoa. Ideally i would also like it to appear in the Interface builder.
我想使用可可向另一个对象发送一个动作。理想情况下,我还希望它出现在界面构建器中。
I've tried the apple documentation, but there is something i'm missing. Adding the following, the interface building only shows the action as a received one.
我试过苹果文档,但我遗漏了一些东西。添加以下内容,界面构建仅将动作显示为接收到的动作。
- (IBAction)setTarget:(id)anObject;
- (IBAction)setAction:(SEL)aSelector;
could someone please give me an example of how to create a sent action. Thanks
有人可以给我一个如何创建发送动作的例子。谢谢
I have the receive action defined in my controller1. I then want my controller2 to be able to send an action to controller1 (like NSButton does). In interface build you can drag a line FROM NSButton to the control that should receive the signal. This functionality can't be limited to just apple objects there, must be a way to do it.
我在我的控制器 1 中定义了接收操作。然后我希望我的控制器 2 能够向控制器 1 发送一个动作(就像 NSButton 那样)。在界面构建中,您可以从 NSButton 拖一条线到应该接收信号的控件。此功能不能仅限于那里的苹果对象,必须有一种方法来做到这一点。
采纳答案by Mark
controller1 and controller2 are a subclasses of NSControl.
controller1 和 controller2 是 NSControl 的子类。
[controller2 sendAction:@selector(receiveAction:) to:controller1];
回答by TALlama
IBAction
is a preprocessor directive that tells Interface Builder "this is something you should know about." It actually evaluates to void
. What that means is that the "actions" are just normal methods that you can call in the normal way:
IBAction
是一个预处理器指令,它告诉 Interface Builder“这是你应该知道的。” 它实际上评估为void
. 这意味着“动作”只是您可以以正常方式调用的正常方法:
[target someMethod:self];
If you want to mimic the behavior you can get from an NSButton
, you can add an outlet for the target and a property for the selector (as a string):
如果你想模仿你可以从 an 获得的行为NSButton
,你可以为目标添加一个出口和一个选择器的属性(作为一个字符串):
@property (nonatomic, retain) IBOutlet id target;
@property (nonatomic, retain) NSString* actionSelectorString;
Which you could then call like this:
然后你可以这样调用:
SEL action = NSSelectorFromString(self.actionSelectorString);
[self.target performSelector:action withObject:self];
If you want your class to wire up the selector via drag-and-drop in Interface Builder, the easiest way is to make your class a subclass of NSControl
(drag a custom view in from the object library, Command-6 to show inspector, choose your class in the top popup). You can then "call" the action by:
如果您希望您的类通过在 Interface Builder 中拖放来连接选择器,最简单的方法是使您的类成为NSControl
(从对象库中拖入自定义视图,Command-6 显示检查器,选择您在顶部弹出窗口中的课程)。然后,您可以通过以下方式“调用”该操作:
- (void) go {
[self sendAction:self.action to:self.target];
}
回答by smorgan
NSButton is an NSControl, and thus has an "action" that you can wire up--a message that is sent when you trigger the control (e.g., clicking on a button). A controller doesn't have an action (what would it mean--how would you cause the message to be sent?), so what you are trying to do doesn't actually make sense.
NSButton 是一个 NSControl,因此有一个可以连接的“动作”——当您触发控件(例如,单击按钮)时发送的消息。控制器没有动作(这意味着什么——您将如何发送消息?),因此您尝试执行的操作实际上没有意义。
If you want controller2 to be able to have controller1 do something, you should create an outlet on controller2 (of type controller1, or some interface it implements), connect it to controller1 in IB, and then you can send any message you want from controller2 to controller1 programatically.
如果您希望控制器 2 能够让控制器 1 做某事,您应该在控制器 2 上创建一个插座(类型为控制器 1,或它实现的某个接口),将其连接到 IB 中的控制器 1,然后您可以从控制器 2 发送您想要的任何消息以编程方式到控制器 1。
回答by e.James
You should be able to do this if your Controller2
class is a subclass of NSControl
. NSControl
defines a "sent action" which is what you are connecting to your target object when you drag from NSButton
in Interface Builder. That being said, there is (was?) a bug in IB 3.0 that prevented the sent action from showing up in IB. See this cocoa-dev threadfor details.
如果您的Controller2
类是NSControl
. NSControl
定义了一个“发送的动作”,它是当您从NSButton
Interface Builder 中拖动时连接到目标对象的内容。话虽如此,IB 3.0 中有(是?)一个错误,阻止了发送的动作在 IB 中显示。有关详细信息,请参阅此 cocoa-dev 线程。
回答by Aron
Actions are always received by an object. If you want to send information in the other direction, you probably want an outlet: look for IBOutlet.
动作总是由对象接收。如果您想向另一个方向发送信息,您可能需要一个出口:寻找 IBOutlet。