swift - 在 TableViewCell 中按下标签时关闭 View

标签 swift uitableview uinavigationcontroller dismiss

我在 TableView 单元格中有一个带有 TapGesture 的标签,如果按下该标签,该标签应该关闭嵌入在导航 Controller 中的 View Controller 。

通常我会这样做:

_ = navigationController?.popViewController(animated: true)

或者

self.dismiss(动画:true)

但这在 TableView 单元格类中不起作用。

如果有人能帮助我,那就太好了!

最佳答案

为了让它工作,你应该将该义务传递给管理你的 tableView 的 viewController 。因此,在你的 tableViewCell 类上面声明一个带有函数的协议(protocol),委托(delegate)(在本例中是持有你的 tableView 的 ViewController)在调用时应该实现该函数。因此,当在 ViewController 内调用委托(delegate)时,如果 ViewController 是模态呈现的,则关闭 ViewController;如果是推送的,则弹出它,因此您上面提到的方法之一应该可以工作。如果您不知道自定义协议(protocol)如何工作,请尝试阅读这篇文章 https://medium.com/@aapierce0/swift-using-protocols-to-add-custom-behavior-to-a-uitableviewcell-2c1f09610aa1

关于swift - 在 TableViewCell 中按下标签时关闭 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005751/

相关文章:

swift - getline() 等价于 Swift

swift - 我可以让#selector 引用 Swift 中的闭包吗?

iphone - 推沃什 : Swift Code

swift - 在 TableViewCell 中添加 CALayer 触发 CALayerInvalid Exception

ios - 隐藏 UINavigationBar 3D touch

xcode - 如果模块不是 main.swift,则“顶层不允许使用表达式”

ios - 使用自定义按钮重新排序 tableview 单元格

ios - 确保 UITableView 以原子方式重新加载的最佳方法是什么?

ios - 从关闭 ViewController 发送信息

ios - 如何在没有导航 Controller 的情况下导航到另一个 Controller ?