如何将(id)发件人添加到以下-(IBAction)?
如何将 (id) 发件人添加到以下代码中?
- (IBAction) gobutton: (UIButton *) button5 {
我尝试的一切都失败了,任何帮助将不胜感激。谢谢。
编辑:我需要在 (IBAction) 中保留 (UIButton *) 按钮 5 引用
How do you add a (id) sender to the following code?
- (IBAction) gobutton: (UIButton *) button5 {
Everything I try fails, any help would be appreciated. Thanks.
EDIT: I need to keep the (UIButton *) button 5 reference in the (IBAction)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以创建一个包含 UIButton 和发送者的简单结构并使用它吗?
结构体myObject
{
UIButton* 按钮5;
发件人 ID;
...
或者,您可以创建自己的 NSObject (可能更可可):
@instance myObject : NSObject
{
...
}
Can you create a simple structure that contains both the UIButton and the sender and use that?
struct myObject
{
UIButton* button5;
id sender;
}
...or, you could create your own NSObject (probably more cocoa-y):
@instance myObject : NSObject
{
...
}
如果我没记错的话,并且如果您按照我认为的方式使用它,
假设您指定了button5作为参数,以指示它响应button5被按下而执行。
If I recall correctly, and if you are using this in the way I think you are,
Assuming that you specified button5 as a parameter to indicate that this executes in response to button5 being pressed.
好吧,首先......除了 Interface Builder 之外,
IBAction
并没有什么特别的意义。基本上:所以每当您看到
IBAction
时,都会想到“void”。它存在的唯一原因是作为一个标志,告诉 Interface Builder 该方法是连接控制操作的有效方法。 Objective-C 编译器不需要知道它,因此它被定义为 void,因为所有“action”方法都返回 void。其次,操作方法也有一个参数,该参数可以是任意数量类型的对象。因此,操作方法应该使用 id 类型作为其参数的类型。这样,它们就可以传递指向任何 Objective-C 对象的指针,而不会导致编译器生成类型检查错误。
因此,通常操作应该像这样工作:
换句话说,
id
几乎就像一个无类型指针,例如当某些函数需要执行时,在 C 中通常会使用void *
指向未知类型的指针。sender
可以是不同类型的控件,因此使用像id
这样的通用内容,然后一旦代码知道它是什么,sender 就会被转换为更具体的内容。不管怎样,绝对没有理由将某些东西定义为具有
IBAction
返回类型,除非您打算在 Interface Builder 中使用该方法作为目标操作。在你的应用程序委托中有一个 IBAction 似乎有点不寻常......Ok, first....
IBAction
doesn't really mean anything special except to Interface Builder. Basically:So whenever you see
IBAction
, think "void". The only reason it's there is as a flag to tell Interface Builder that a method is a valid method to connect control actions to. The Objective-C compiler doesn't need to know about it and so it's defined to void since all "action" methods return void.Second, action methods also have one argument which could be an object of any number of types. Because of this, action methods are supposed to use type
id
as the type for their argument. That way they can be passed a pointer to any Objective-C object without causing the compiler to generate a type checking error.So usually actions should work something like this:
In other words, an
id
is almost like an untyped pointer like avoid *
is routinely used in C when some function needs to take a pointer to something of unknown type.sender
could be different types of control, so something generic likeid
is used then sender is cast to something more specific once the code knows what it is.Anyway, there is absolutely no reason to define something as having a return type of
IBAction
unless you are going to use that method as a target action in Interface Builder. Having an IBAction in your app delegate seems kind of unusual....目前尚不清楚您要做什么,但大多数操作如下所示:
It's not clear what you are trying to do but most actions look like:
操作的第一个参数始终是发送者(您可以根据需要指定类型和名称)。
如果方法是按钮的操作,则第一个参数将是按钮。如果该方法是多个按钮的操作,那么第一个参数将允许您确定点击了哪个按钮(如 Leper 所描述的)。
您实际上想解决什么问题?
有一些技术可以将信息传递给操作方法。例如,如果您有一个出现在表格视图单元格上的按钮,并对每个单元格执行相同的操作,那么在操作方法中,您可能希望能够确定哪个单元格的按钮被点击。
The first parameter to an action is always the sender (you can specify the type and name as appropriate).
If a method is the action for a button, then the first parameter will be the button. If that method is the action for several buttons, then the first parameter will allow you to determine which button was tapped (as Leper describes).
What problem are you actually trying to solve?
There are techniques for passing information to the action method. For example, if you have a button that appears on a table view cell and performs the same action for every cell, then in the action method, you would want to be able to determine which cell's button was tapped.
如何在用户触摸控件之前获取发送者的 ID?
找到了!设置标签并使用 viewWithTag。
How can I get the id of the sender before the user touches the control?
Found it! Set a tag and the use viewWithTag.