问题:我在complaintsController中有一个表格视图,里面有“打开”按钮。每当我按下打开按钮时,我都会使用该行的数据来查看DetailComplaintViewController,但是当我返回到ComplaintController并选择一个不同的按钮时,我会看到之前选择的相同数据。
注意 - 我已经从tableView的单元格中的按钮创建了一个segue。
这是我用来从ComplaintController转到DetailComplaintController的代码。
var passRow = 0
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let billCell = tableView.dequeueReusableCell(withIdentifier: "complaintCell") as! ComplaintTableViewCell
billCell.openBtn.tag = indexPath.row
billCell.openBtn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
return billCell
}
func btnClicked(sender: UIButton) {
passRow = sender.tag
print("Position......\(sender.tag)")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let rVC = segue.destination as? DetailComplaintViewController
rVC?.issueType = self.complaintListArray[passRow].issueType
rVC?.issuedescription = self.complaintListArray[passRow].description
rVC?.issuedate = self.complaintListArray[passRow].issueDate
}
答案 0 :(得分:1)
问题是在btnClicked
函数之前会调用segue的准备工作,这就是为什么你没有得到正确的数据。
快速解决您的情况将是准备segue方法中的按钮标记,如下所示:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let button = sender as? UIButton else {
print("Segue was not called from button")
return
}
let row = button.tag
let rVC = segue.destination as? DetailComplaintViewController
rVC?.issueType = self.complaintListArray[row].issueType
rVC?.issuedescription = self.complaintListArray[row].description
rVC?.issuedate = self.complaintListArray[row].issueDate
}
其他选项是从按钮中删除segue并在视图控制器上创建它,并在btnClicked
方法中以this answer