0

我需要通过与@选择参数,在这里传递的参数是,我需要使用选择调用的方法:如何使用@selector

-(void)clickedInfo:(NSString *)itemIndex{ 
// some work with itemIndex 
} 

我知道,我能做的就是用中间方法如here所述。

这种方法在我的情况下不起作用,因为im将目标添加到collectionView的cellForItemAtIndexPath方法中的uibutton。

我需要传递给clickedInfo方法的参数是indexPath.row ,我无法在中间方法中获取此参数。

Thanx提前

回答

2

可以使用performSelector:withObject:选择传递对象。

例子:

[self performSelector:@selector(clickedInfo:) withObject:myIndex]; 

- (void) clickedInfo:(NSString *)itemIndex{ 
// some work with itemIndex 
} 

编辑:应该只是@selector(clickedInfo:)而不是我收到。

编辑:使用@newacct的建议下,我建议做类似下面的内容:

- (UITableViewCell *)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath 
{ 
    button.tag = indexPath.row; 
    [button performSelector:@selector(clickedInfo:)]; 
    // or 
    [button addTarget:self action:@selector(clickedInfo:) forControlEvents:UITouchUpInside]; 
} 

- (void) clickedInfo:(id)sender 
{ 
    int row = sender.tag; 
    // Do stuff with the button and data 
} 
+0

这是不对的 –

+0

你是对的这是不正确的,更新我的答案,但其他人已经给出了相同的例子。 –

+0

这个问题表明他们想要设置一个按钮的动作 – newacct

1

这是写给很多地方,但它是比较容易回答,而不是有一点你:

[someObject performSelector:@selector(clickedInfo:) withObject:someOtherObject]; 

其中someObject是接收机和someOtherObject是传递给clickedInfo

+0

这个问题表明他们想要设置一个按钮的动作 – newacct

3

所以y中的参数你想存储一些可以通过按钮的动作访问的信息。一些选项是:

  • 使用控件的标签属性。 (只能存储一个整数)
  • 子类UIButton并使用该类的按钮。这个类可以有一个存储信息的字段。
  • 使用关联对象(关联引用)将对象附加到按钮。这是最一般的解决方案。
+0

标签属性似乎是他想要的一个好主意。只需将标签设置为'indexPath。行“而不是将其作为参数传递。 –

+0

我用uibutton的titlelabel字段来存储字符串值,因为我不必显示任何使用它。感谢名单 – onuryilmaz

相关问题