如何将(id)发件人添加到以下-(IBAction)?

发布于 2024-08-18 01:33:27 字数 185 浏览 4 评论 0原文

如何将 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撕心裂肺的伤痛 2024-08-25 01:33:28

您可以创建一个包含 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
{
...
}

毁虫ゝ 2024-08-25 01:33:27

如果我没记错的话,并且如果您按照我认为的方式使用它,

- (IBAction) gobutton: (id) sender {
if(sender == button5)
   //do something...
else
   //do something else...
}

假设您指定了button5作为参数,以指示它响应button5被按下而执行。

If I recall correctly, and if you are using this in the way I think you are,

- (IBAction) gobutton: (id) sender {
if(sender == button5)
   //do something...
else
   //do something else...
}

Assuming that you specified button5 as a parameter to indicate that this executes in response to button5 being pressed.

So要识趣 2024-08-25 01:33:27

好吧,首先......除了 Interface Builder 之外,IBAction 并没有什么特别的意义。基本上:

#define IBAction void

所以每当您看到 IBAction 时,都会想到“void”。它存在的唯一原因是作为一个标志,告诉 Interface Builder 该方法是连接控制操作的有效方法。 Objective-C 编译器不需要知道它,因此它被定义为 void,因为所有“action”方法都返回 void。

其次,操作方法也有一个参数,该参数可以是任意数量类型的对象。因此,操作方法应该使用 id 类型作为其参数的类型。这样,它们就可以传递指向任何 Objective-C 对象的指针,而不会导致编译器生成类型检查错误。

因此,通常操作应该像这样工作:

- (IBAction)myAction:(id)sender {
    if (sender == self.someButton) {
        UIButton *button = (UIButton *)sender;
        ...
        return;
    } else if (sender == self.someControl) {
        UIControl *control = (UIControl *)sender;
        ...
        return;
    }
}

换句话说,id 几乎就像一个无类型指针,例如当某些函数需要执行时,在 C 中通常会使用 void *指向未知类型的指针。 sender 可以是不同类型的控件,因此使用像 id 这样的通用内容,然后一旦代码知道它是什么,sender 就会被转换为更具体的内容。

不管怎样,绝对没有理由将某些东西定义为具有 IBAction 返回类型,除非您打算在 Interface Builder 中使用该方法作为目标操作。在你的应用程序委托中有一个 IBAction 似乎有点不寻常......

Ok, first.... IBAction doesn't really mean anything special except to Interface Builder. Basically:

#define IBAction void

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:

- (IBAction)myAction:(id)sender {
    if (sender == self.someButton) {
        UIButton *button = (UIButton *)sender;
        ...
        return;
    } else if (sender == self.someControl) {
        UIControl *control = (UIControl *)sender;
        ...
        return;
    }
}

In other words, an id is almost like an untyped pointer like a void * 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 like id 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....

烧了回忆取暖 2024-08-25 01:33:27

目前尚不清楚您要做什么,但大多数操作如下所示:

- (IBAction) gobutton: (id)sender;

It's not clear what you are trying to do but most actions look like:

- (IBAction) gobutton: (id)sender;
樱娆 2024-08-25 01:33:27

操作的第一个参数始终是发送者(您可以根据需要指定类型和名称)。

如果方法是按钮的操作,则第一个参数将是按钮。如果该方法是多个按钮的操作,那么第一个参数将允许您确定点击了哪个按钮(如 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.

陌上芳菲 2024-08-25 01:33:27

如何在用户触摸控件之前获取发送者的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文