RubyMotion - 按下按钮传递参数

时间:2017-04-06 15:02:59

标签: ruby-on-rails ruby rubymotion

我有一个单元格表,每个单元格都显示一个单独的对象(链)。我正在尝试为每个单元格添加一个按钮,它将该单元格的链作为参数调用,或者至少以某种方式将其传递给它。我的代码如下:

directive.compile = function(originalCompile) {
  return function() {
    var oldLink = originalCompile.apply(this, arguments);

    return function($scope, element, attrs) {
      // custom code for link here
      return oldLink.apply(this, arguments)
    }
  }
}(directive.compile);

我已经阅读了以下内容:

How to pass parameters via the selector/action?

这提供了两种解决方案,这两种解决方案都不适用于我。第一个是设置一个实例变量来等于变量,但是由于我有多个单元格,我需要多个实例变量,这样会很快变得混乱。

提出的另一个解决方案是使用tag属性,我试图在我的代码中使用它,但这会引发错误:

def select_chain(sender)
  unselect_old_chain
  chain = Chain.find(sender.tag)
  chain.selected = true
  chain.save
  cdq.save
end

def tableView(tableView, cellForRowAtIndexPath: indexPath)
  cell = tableView.dequeueReusableCellWithIdentifier(ChainCell::ID, forIndexPath: indexPath)
  cell.label_title.text = data_source[indexPath.row].title
  cell.chain = data_source[indexPath.row]
  button = UIButton.buttonWithType(UIButtonTypeCustom)
  button.setTitle 'Go!', forState: UIControlStateNormal
  button.tag = cell.chain.id
  button.addTarget self, action: :select_chain, forControlEvents: UIControlEventTouchUpInside
  button.backgroundColor= UIColor.greenColor
  button.frame = CGRectMake(cell.frame.origin.x + 210, cell.frame.origin.y + 10, 80, 20)
  cell.contentView.addSubview(button)
  cell
end

据我所知,我只是使用整数来尝试查找Chain with Chain.find(sender.tag),但这不起作用。

非常感谢任何关于如何使这种方法有效或任何其他方法的建议。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为你需要这样写:

button.addTarget self, action: 'select_chain:', forControlEvents: UIControlEventTouchUpInside

请勿使用符号作为选择器。

相关问题