ios - 在使用标识符实例化 Storyboard时使用委托(delegate)传递数据的问题

标签 ios swift delegates

我确实浏览了其他用户的各种帖子,这些帖子似乎处理类似的委托(delegate)问题,但坦率地说,我无法从他们那里获取足够的信息来处理我当前的问题..:) 我的问题是这样的..

我有一个加载了几个表格 View 单元格的表格 View 。单击这些单元格上的某个编辑图标会将我带到另一个 View Controller 。 tableview 单元格上还有一个特定的 productName,它是一个标签。

现在的问题是当我点击编辑按钮时,我想把这个 productName 带到另一个 View Controller ,这样我就可以在导航栏的后退按钮中显示它。在 tableviewcell 类中,我做了一个这样的委托(delegate)......

protocol MyCellDelegate {
    func editBtnTapped(cell: ProductsTableViewCell)
}

然后点击编辑按钮,就像这样......

   @IBAction func editBtnTapped(_ sender: Any) {
        if let _ = delegate {
            delegate?.editBtnTapped(cell: self)
        }
    }

在加载此 tableviewcell 的 tableviewcontroller 类中,我调用了 editBtnTapped 方法......

func editBtnTapped(cell: ProductsTableViewCell) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier")
    let navController = UINavigationController(rootViewController: controller)
    self.present(navController, animated: true, completion: nil)

}

这里我展示了另一个 View Controller ,我必须将我的 productName 传递给这个 View Controller tableviewcell 类。而这我无法实现..:(

最佳答案

您可以在 func editBtnTapped() 中传递 tableSelectedRowIndex 而不是传递单元格类引用。通过这个你可以很容易地实现你的目标。你可以这样做

像这样在 tableViewCell 类中声明协议(protocol)

protocol MyCellDelegate {
    func editBtnTapped(SelectedIndex : Int)
}

还在tableViewCell类中创建一个编辑按钮的IBOutlet

@IBOutlet weak var btnEdit: UIButton!

并创建它的 Action

@IBAction func editBtnTapped(_ sender: UIButton) {
        if let _ = delegate {
            delegate?.editBtnTapped(SelectedIndex: sender.tag)
        }
    }

然后在cellForRowAtIndexPath方法中将editbtton标签值设置为indexPath.row

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "customCell"
    var cell : CustomCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CustomCell?
    if (cell == nil) {

        cell = Bundle.main.loadNibNamed("CustomCell", owner: nil, options: nil)?[0] as? CustomCell
    }

    cell.btnEdit.tag = indexPath.row
    cell.delegate = self
    return cell!
}

    func editBtnTapped(SelectedIndex : Int) {

        let productName = // get name of product like you display in cellForRowAtIndexPath

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "editProductsIdentifier")
        controller.productName = // Pass your product name
        let navController = UINavigationController(rootViewController: controller)
        self.present(navController, animated: true, completion: nil)

    }

如果您发现任何困难,请告诉我。

关于ios - 在使用标识符实例化 Storyboard时使用委托(delegate)传递数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46155773/

相关文章:

objective-c - 加载plist时数组下标不是整数

ios - 使用 Google map sdk 在 ios 9 中获取当前位置

macos - 从 OS X 使用 DualShock 4

c# - 作为方法调用参数的函数

ios - 如何使用文件路径的引用获取图库文件路径并显示图像

ios - UIMenucontroller 与 uitableviewcell 上的 uitextview

swift - 将 NSMutablearray 的特定索引数据复制到 swift 中的另一个索引数据

ios - Delgator的delegate属性指向两个Delegators而不是Delegate类

jquery - 防止重叠 HTML 元素触发事件

javascript - 尝试在 create-react-native-app 项目中使用 react-native-fs (expo)