将参数传递给addTarget:action:forControlEvents [英] Passing parameters to addTarget:action:forControlEvents

查看:532
本文介绍了将参数传递给addTarget:action:forControlEvents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用addTarget:action:forControlEvents,如下所示:



  [newsButton addTarget:self 
action:@selector(switchToNewsDetails)
forControlEvents:UIControlEventTouchUpInside];



,我想传递参数给我的选择器switchToNewsDetails。
我成功的唯一办法是通过写(id)发送者:

  action:@selector(switchToNewsDetails:)



但我试图传递变量,如整数值。以这种方式写入不起作用:



  int i = 0; 
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i)
forControlEvents:UIControlEventTouchUpInside];



这种写法无效:



  int i = 0; 
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i :)
forControlEvents:UIControlEventTouchUpInside];



任何帮助将不胜感激:)

解决方案

  action:@selector(switchToNewsDetails :) 

此处不传递参数到 switchToNewsDetails:方法。你只需创建一个选择器,使按钮能够调用它当某些行动发生时(在你的情况下触摸)。控件可以使用3种类型的选择器来响应动作,它们都具有其参数的预定义含义:


  1.   action:@selector(switchToNewsDetails)

    / li>

  2. 有一个指示发送消息的控件的参数

     选择器(switchToNewsDetails :) 


  3. 有两个参数指示发送消息和事件的控件触发消息:

     操作:@selector(switchToNewsDetails:event :) 


目前尚不清楚您尝试做什么,每个按钮的索引可以执行以下操作:


  1. 为每个按钮设置一个等于所需索引的标签属性
  2. $ b $在 switchToNewsDetails:方法中,您可以获取该索引并打开相应的缩写:

       - (void)switchToNewsDetails:(UIButton *)sender {
    [self openDetails:sender.tag];
    //或在这里打开逻辑
    }



I am using addTarget:action:forControlEvents like this:

[newsButton addTarget:self
action:@selector(switchToNewsDetails)
forControlEvents:UIControlEventTouchUpInside];

and I would like to pass parameters to my selector "switchToNewsDetails". The only thing I succeed in doing is to pass the (id)sender by writing:

action:@selector(switchToNewsDetails:)

But I am trying to pass variables like integer values. Writing it this way doesn't work :

int i = 0;
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i)
forControlEvents:UIControlEventTouchUpInside];

Writing it this way does not work either:

int i = 0;
[newsButton addTarget:self
action:@selector(switchToNewsDetails:i:)
forControlEvents:UIControlEventTouchUpInside];

Any help would be appreciated :)

解决方案

action:@selector(switchToNewsDetails:)

You do not pass parameters to switchToNewsDetails: method here. You just create a selector to make button able to call it when certain action occurs (touch up in your case). Controls can use 3 types of selectors to respond to actions, all of them have predefined meaning of their parameters:

  1. with no parameters

    action:@selector(switchToNewsDetails)
    

  2. with 1 parameter indicating the control that sends the message

    action:@selector(switchToNewsDetails:)
    

  3. With 2 parameters indicating the control that sends the message and the event that triggered the message:

    action:@selector(switchToNewsDetails:event:)
    

It is not clear what exactly you try to do, but considering you want to assign a specific details index to each button you can do the following:

  1. set a tag property to each button equal to required index
  2. in switchToNewsDetails: method you can obtain that index and open appropriate deatails:

    - (void)switchToNewsDetails:(UIButton*)sender{
        [self openDetails:sender.tag];
        // Or place opening logic right here
    }
    

这篇关于将参数传递给addTarget:action:forControlEvents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆