将参数传递给@selector 方法

Passing arguments to @selector method(将参数传递给@selector 方法)
本文介绍了将参数传递给@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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)