问题描述
我在表格视图中添加了一个 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
}
并且通过 indexPath 你可以得到你需要的对象.
and with the indexPath you can get the object you need.
这篇关于将参数传递给@selector 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!