![](/img/trans.png)
[英]Swift: UIButton within a custom UITableViewCell 'Unrecognised selector sent to instance' error
[英]Identifying UIButton within UITableViewCell not working - Swift
我试图通过在单元格中按下的按钮来标识单元格索引。 完成此操作后,我试图将信息传递给另一个viewController
原始文章可以在这里找到: 检测在表视图中按下的uibutton:Swift最佳实践
我的代码当前如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! questionCell
//indicates which button was pressed
myCell.button.tag = indexPath.row
myCell.button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
return myCell
}
var selectedRow = Int()
//sets selected row variable to the row that was selected
func buttonClicked(sender:UIButton) {
selectedRow = sender.tag
}
//sets the id of the question that was selected in the answers view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destinationView: AFeedTableViewController = segue.destinationViewController as! AFeedTableViewController
destinationView.currentQuestionId = idResults[selectedRow]
}
我的问题是prepareForSegue函数在buttonClicked函数之前被调用,因此没有更新我传递给另一个视图控制器的值。 谁能解释如何解决这个问题? 谢谢
编辑:单击按钮时,将执行segue
假设您的segue已连接到按钮,则不需要额外的目标。 您可以通过prepareForSegue:sender:
的发件人对象访问按钮prepareForSegue:sender:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let button = sender as? UIButton else { return }
guard let destinationViewController = segue.destinationViewController as? AFeedTableViewController else { return }
destinationViewController.currentQuestionId = idResults[button.tag]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.