gpt4 book ai didi

ruby-on-rails - RubyMotion - 在按下按钮时传递参数

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:27 25 4
gpt4 key购买 nike

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

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

我已阅读以下内容:

How to pass parameters via the selector/action?

这提供了两种解决方案,但都不适合我。第一个是将实例变量设置为等于变量,但是由于我有多个单元格,所以我需要多个实例变量,这很快就会变得困惑。

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ChainsTable select_chain]: unrecognized selector sent to instance 0x1169b9000'

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

非常感谢任何关于如何使此方法起作用或任何其他方法的建议。

提前致谢。

最佳答案

我认为你需要这样写:

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

不要使用符号作为选择器。

关于ruby-on-rails - RubyMotion - 在按下按钮时传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com