【问题标题】:Passing arguments to @selector method将参数传递给@selector 方法
【发布时间】:2011-03-09 13:54:07
【问题描述】:

我在表格视图中添加了UIButton。现在,当我单击特定按钮时,我需要获取与该行对应的对象的详细信息。

[button addTarget:self 
           action:@selector(invite:contactmail:) 
 forControlEvents:UIControlEventTouchUpInside];  

我在按钮上使用了这种方法;如何将参数传递给此选择器方法?或者有没有其他方法可以在单击此按钮时将参数传递给此方法?

【问题讨论】:

  • 使用UIButtontag 值来发布关于行的标识符,在动作处理程序方法中,您可以进一步读取tag 值,您不需要发布其他任何内容,因为您已经在 controller-layer 上,并且您可以访问 model-layer 以及所有数据。

标签: ios objective-c cocoa-touch uibutton selector


【解决方案1】:

作为操作添加到 UIControl 的方法可以有 3 个不同的签名

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender event:(UIEvent *)event

你不能传递你自己的对象。通常可以在发送者(您的按钮)对象的帮助下获取您尝试传递的任何对象。

如果您在 tableviewcell 中有一个按钮,您可以使用类似的方法来获取单元格的索引路径。

- (IBAction)buttonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    CGPoint buttonOriginInTableView = [button convertPoint:CGPointZero toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];

    // do something
}

并且通过 indexPath 你可以得到你需要的对象。

【讨论】:

    【解决方案2】:

    不,不是。点击事件是

    -(IBAction) fnc:(id)sender;
    

    如果你想调用另一个方法,你需要在那个表单的函数中这样做。

    -(IBAction) fnc:(id) sender
    {
       UIButton *b = (UIButton*) sender;
       [self otherFnc:b arg:arg1 arg2:arg2];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2017-01-05
      相关资源
      最近更新 更多