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 方法
基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
