Passing arguments to @selector method(将参数传递给@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
}
并且通过 indexPath 你可以得到你需要的对象.
and with the indexPath you can get the object you need.
这篇关于将参数传递给@selector 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将参数传递给@selector 方法


基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01