将参数传递给@selector方法 [英] Passing arguments to @selector method

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

问题描述

我在表视图中添加了 UIButton 。现在当我单击一个特定的按钮,我需要获得对应的行对象的详细信息。

I have added a UIButton in a table view. Now when I click a particular button, I need to get the details of the object corresponding to that row.

[button addTarget:self 
           action:@selector(invite:contactmail:) 
 forControlEvents:UIControlEventTouchUpInside];  

我已经使用此方法作为按钮;如何传递参数到这个选择器方法?或者在点击此按钮时,是否还有其他方法可以向此方法传递参数?

I have used this method for the button; how can I pass arguments to this selector method? Or is there any other way to pass arguments to this method when this button is clicked?

推荐答案

UIControl可以有3个不同的签名

methods you add as actions to a UIControl can have 3 different signatures

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

不能通过你自己的对象。通常,有可能得到任何你想要通过发送者(你的按钮)对象的帮助下传递的对象。

you can't pass your own object. Often it is possible to get whatever object you are trying to pass with the help of the sender (your button) object.

如果您在tableviewcell中有一个按钮,您可以使用类似这样的方法来获取单元格的索引路径。

If you have a button in a tableviewcell you could use something like this to get the indexpath of the cell.

- (IBAction)buttonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];

    // do something
}

你可以得到你需要的对象。

and with the indexPath you can get the object you need.

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

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