【问题标题】:Data sent on button press persists when different button is pressed当按下不同的按钮时,按钮按下时发送的数据仍然存在
【发布时间】:2018-01-27 20:03:46
【问题描述】:

问题:我在complaintController 中有一个表格视图,其中有“打开”按钮。每当我按下打开按钮时,我都会使用该行的数据转到 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
                }

【问题讨论】:

  • 你能更清楚地解释你所面临的问题吗?
  • 即使我选择了不同的按钮,我之前选择的数据也会显示出来
  • 好吧,这意味着即使您在单元格中选择了不同的打开按钮,您也会得到错误的数据?
  • 是的,我得到了错误的数据
  • 然后获取该行的索引路径并根据该行从数组中获取值并从 Prepareforsegue 方法发送

标签: ios swift


【解决方案1】:

问题是在你的 btnClicked 函数之前会调用准备 segue,这就是你没有得到正确数据的原因。

快速解决您的情况的方法是在 prepare for 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 方法中执行该 segue,这在 this answer 中进行了解释

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多