将不同的参数传递给 IBAction

发布于 2024-08-19 12:12:30 字数 165 浏览 5 评论 0原文

我的 iPhone 应用程序有很多按钮,我希望所有按钮都调用相同的方法,但参数不同。

例如,我想点击一个按钮来调用带有参数 @"foo" 的方法 myMethod:,第二个按钮应该调用相同的方法,但带有参数 @“酒吧”

My iPhone app has many buttons and I want all the buttons to call the same method, but with different parameters.

For example I want tapping one button to call the method myMethod: with the argument @"foo", and a second button should call the same method but with argument @"bar".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

淡莣 2024-08-26 12:12:30

所谓的“IBActions”必须具有以下签名之一:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

您不能添加任何其他参数。不过,您可以使用 sender (在您的情况下为button1或button2)来获取参数:

-(void)actionWithSender:(UIButton*)sender {
   NSString* parameter;
   if (sender.tag == 1)   // button1
     parameter = @"foo";
   else                   // button2
     parameter = @"bar";
   ...
}

The so called "IBActions" must have one of these signatures:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

You cannot add any other parameters. Nevertheless you can use sender (which is button1 or button2 in your case) to get the parameter:

-(void)actionWithSender:(UIButton*)sender {
   NSString* parameter;
   if (sender.tag == 1)   // button1
     parameter = @"foo";
   else                   // button2
     parameter = @"bar";
   ...
}
木槿暧夏七纪年 2024-08-26 12:12:30

您无法添加额外参数的真正原因是 UIKIT 会将参数推送到堆栈上。
所以唯一的方法就是使用标签。
一种肮脏的方法是将指针转换为 int 并用它标记按钮:

myStruct params;
// fill params:
params.x=....
params.y=....
params.z=....


UIButton * btn = [UIButton......]; // create or use one from XIB
btn.tag = (int)¶ms;

... in Call back:
-(IBActions) doIt:(id)sender
{
  myStruct * paramsPtr = (myStruct*)tag;
  int i = paramsPtr->x;

注意:参数必须保持静态..或使用 malloc 分配(越来越难看的代码...)。
不要使用本地变量:它将在堆栈上分配,因此在退出设置方法后将被删除。

the real reason You cannot add additional parameter is that UIKIT will push params on the stack.
so the only way is to use tags.
A DIRTY way can be to convert a pointer to int and tagging the button with it:

myStruct params;
// fill params:
params.x=....
params.y=....
params.z=....


UIButton * btn = [UIButton......]; // create or use one from XIB
btn.tag = (int)¶ms;

... in Call back:
-(IBActions) doIt:(id)sender
{
  myStruct * paramsPtr = (myStruct*)tag;
  int i = paramsPtr->x;

NOTE: params MUST be keep static .. or allocate using malloc (more and more ugly code...).
DO NOT use a local var: it will be allocated on stack so will be removed after exiting from the setup method.

拔了角的鹿 2024-08-26 12:12:30

为您的各种 UIButton 实例提供不同的 tag 属性值。

在您的 IBAction 方法 -myMethod: 中,您可以执行以下操作:

- (void) myMethod:(id)sender {
    switch (sender.tag) {
        case (firstButtonTag):
           doFooStuff;
           break;
        case (secondButtonTag):
           doBarStuff;
           break;
        // etc.
    }
}

firstButtonTagsecondButtonTag 可以是如果您想让它易于维护,请存储在 enum 中。

Give your various UIButton instances different tag property values.

In your IBAction method -myMethod:, you might then do something like:

- (void) myMethod:(id)sender {
    switch (sender.tag) {
        case (firstButtonTag):
           doFooStuff;
           break;
        case (secondButtonTag):
           doBarStuff;
           break;
        // etc.
    }
}

The values firstButtonTag and secondButtonTag can be stored in an enum if you want to make this easy to maintain.

救星 2024-08-26 12:12:30

您不能通过 IBAction 传递参数。我通常做的就是为按钮赋予 IB 中唯一的标签。该标签是一个整数值,因此我然后使用一个简单的查找表将标签转换为某个值。

在本例中,有三个按钮,但标签为 1 到 3:

- (IBAction) buttonPressed: (UIButton*) sender
{
    static const NSString* names = { @"Foo", @"Bar", @"Baz" };
    id tag = [sender tag];
    if (tag >= 1 && tag <= 3) {
        NSLog(@"Button pressed is %@", names[tag]);
    }
}

You can't pass parameters through an IBAction. What I usually do is give the buttons the unique tag in IB. THe tag is an integer value so I u then use a simple lookup table to convert the tag to some value.

In this case, three buttons but tags 1 to 3:

- (IBAction) buttonPressed: (UIButton*) sender
{
    static const NSString* names = { @"Foo", @"Bar", @"Baz" };
    id tag = [sender tag];
    if (tag >= 1 && tag <= 3) {
        NSLog(@"Button pressed is %@", names[tag]);
    }
}
情丝乱 2024-08-26 12:12:30

(id)Sender 显示无论您在 UIButton 单击事件上传递什么,都会直接传递给此方法,无论它是什么类型,它都会自动采用,就像您传递按钮标签一样,它会将按钮标签作为 sender.tag 等

(id)Sender is shows that whatever u pass on UIButton click event is directly pass to this method and no matter that what type it is , it take automatically like if you pass button tag then it take button tag as sender.tag etc

看海 2024-08-26 12:12:30

正如其他人提到的,您无法将自定义参数传递到操作方法中。如果您不喜欢使用标签的解决方案,您还可以使用自定义类对 UIButton 进行子类化,并在其中添加参数。 (我不会打扰,只使用标签)

As others have mentioned you cannot pass your custom parameter into action method. If you do not like the solution using tags you may also subclass UIButton with your custom class and add your parameter there. (By I wouldn't bother and just use tags)

陪你搞怪i 2024-08-26 12:12:30

你不知道。唯一的参数是发送者对象,您可以使用它来产生不同的行为,但我要做的是定义 2 个操作方法,它们只是依次调用具有不同参数的相同方法,即您将拥有

- (IBAction)button1:(id)sender
{
    [self doStuff:kButton1];
}

- (IBAction)button2:(id)sender
{
    [self doStuff:kButton2];
}

- (void)doStuff:(ParamType)param;
{
    ...
}

:为了捍卫该方法(没有双关语),我想补充一点,当您使用 Interface Builder 查看 UI 时会更清楚地看到不同的按钮实际上有不同的效果,这更难判断它们是否都调用了whateverAction:

You don't. The only parameter is the sender object, which you may use to have a different behavior, but what I'd do is define 2 action methods, which simply in turn call the same method with a different parameter, i.e. you'd have:

- (IBAction)button1:(id)sender
{
    [self doStuff:kButton1];
}

- (IBAction)button2:(id)sender
{
    [self doStuff:kButton2];
}

- (void)doStuff:(ParamType)param;
{
    ...
}

In defense of that method (no pun intended), I'd add that it makes clearer when you review your UI with Interface Builder to see that different buttons actually have different effects, which is harder to tell if they all call whateverAction:

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