objective-c 如何将参数传递给此函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/450222/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:03:08  来源:igfitidea点击:

How can I pass a parameter to this function?

iphoneobjective-c

提问by Vijayeta

I have the following code:

我有以下代码:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];

- (void)replyAll:(NSInteger)tid {
// some code
}

How can I send a parameter to the ReplyAllfunction?

如何向ReplyAll函数发送参数?

回答by Rob Fonseca-Ensor

The replyAll method should accept (id)sender. If a UIButton fired the event, then that same UIButton will be passed as the sender. UIButton has a property "tag" that you can attach your own custom data to (much like .net winforms).

replyAll 方法应该接受 (id)sender。如果 UIButton 触发了该事件,则相同的 UIButton 将作为发送方传递。UIButton 有一个属性“标签”,您可以将自己的自定义数据附加到(很像 .net winforms)。

So you'd hook up your event with:

因此,您可以将您的活动与:

[replyAllBtn addTarget:self.target action:@selector(ReplyAll:) forControlEvents:UIControlEventTouchUpInside];
replyAllBtn.tag=15;

then handle it with:

然后处理它:

(void) ReplyAll:(id)sender{
    NSInteger *tid = ((UIControl*)sender).tag;
    //...

回答by MLefrancois

A selector function will normally be defined as such:

选择器函数通常定义为:

- (void) ReplyAll:(id)sender;

So the only parameter an action will ever receives is the actual control that called it. You could just add a property to your control that can be read in replyAll

因此,动作将接收的唯一参数是调用它的实际控件。您可以在您的控件中添加一个可以在 replyAll 中读取的属性

回答by lostInTransit

If you want to send an int value, set the tag of the button = the int value you want to pass. Then you can access the tag value of the button to get the int you wanted.

如果你想发送一个int值,设置按钮的标签=你想传递的int值。然后你可以访问按钮的标签值来获取你想要的 int 值。

NSInteger is not a pointer. Try this

NSInteger 不是指针。尝试这个

NSInteger tid = sender.tag;

回答by Vijayeta

It's working now :D.

它现在正在工作:D。

{
NSInteger tid = [sender tag];
}

回答by Thomas Tempelmann

The MVC model used in Cocoa works differently. Basically, the idea is that a control (=view) such as a button only lets a function know it was pressed, not knowing what this means. The function then has to know all the dynamics and dependencies. In your case, it's the function that has to find the parameter. To accomplish that, you'll "bind" other objects to the function (= controller).

Cocoa 中使用的 MVC 模型的工作方式不同。基本上,这个想法是像按钮这样的控件(=视图)只让一个功能知道它被按下了,不知道这意味着什么。然后该函数必须知道所有的动态和依赖关系。在您的情况下,它是必须找到参数的函数。为此,您需要将其他对象“绑定”到函数(= 控制器)。

I suggest you read a few Cocoa tutorials first if you want to get ahead with iPhone programming.

如果你想在 iPhone 编程方面取得领先,我建议你先阅读一些 Cocoa 教程。

回答by Dan Keen

There's a few good ways to do this. The two most commonly implemented would be to have the controller (who's receiving the action) know about possible senders, or having the sender itself have a method that you end up using to determine the proper behavior.

有一些很好的方法可以做到这一点。两种最常见的实现方式是让控制器(接收操作的人)知道可能的发件人,或者让发件人本身拥有一种您最终用来确定正确行为的方法。

The first (my preferable way, but it's easy to argue the opposite) would be implemented like such:

第一个(我更喜欢的方式,但很容易反驳)将像这样实现:

@interface Controller : NSObject {
    UIButton *_replyToSender;
    UIButton *_replyToAll;
}
- (void)buttonClicked:(id)sender;
@end

@implementation Controller
- (void)buttonClicked:(id)sender {
    if (sender == _replyToSender) {
         // reply to sender...
    } else if (sender == _replyToAll) {
         // reply to all...
    }
}
@end

The second way would be implemented in a manner such as:

第二种方式将以如下方式实施:

typedef enum {
    ReplyButtonTypeSender = 1,
    ReplyButtonTypeAll,
} ReplyButtonType;

@interface Controller : NSObject {
}
- (void)buttonClicked:(id)sender;
@end

@interface MyButton : UIButton {
}
- (ReplyButtonType)typeOfReply;
@end

@implementation Controller
- (void)buttonClicked:(id)sender {
    // You aren't actually assured that sender is a MyButton, so the safest thing
    // to do here is to check that it is one.
    if ([sender isKindOfClass:[MyButton class]]) {
        switch ([sender typeOfReply]) {
            case ReplyButtonTypeSender:
                // reply to sender...
                break;
            case ReplyButtonTypeAll:
                // reply to all...
                break;
        }
    }
}
@end