gpt4 book ai didi

ios - 识别 UITableViewCell 中的 UIButton 不起作用 - Swift

转载 作者:行者123 更新时间:2023-11-29 01:20:15 26 4
gpt4 key购买 nike

我试图通过在单元格内按下的按钮来识别单元格索引。完成此操作后,我尝试将信息传递给另一个 viewController

原始帖子可以在这里找到: detecting uibutton pressed in tableview: Swift Best Practices

我的代码目前看起来像这样:

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]
}

我的问题是在buttonClicked函数之前调用prepareForSegue函数,因此不会对我传递给其他 View Controller 的值进行更新。谁能解释如何解决这个问题?谢谢

编辑:单击按钮时,将执行转场

最佳答案

假设您的转场已连接到按钮,您不需要额外的目标。您可以通过 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]
}

关于ios - 识别 UITableViewCell 中的 UIButton 不起作用 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701417/

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