ios - didSelectRowAtIndexPath 时关闭 View 并设置数据

标签 ios swift

我有两个 View Controller 、委托(delegate)和一个字符串数组

我有一个委托(delegate)函数,旨在将第一个 VC 上的标签设置为第二个 VC 的 TableView 上选定的行中的字符串:

openSelected(selected: String, viewController: UIVIewController){
label.text = selected
}

这是我在第二个 VC 上的 didSelectRow 函数:

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){

   var selected = savedStrings[indexPath.row]
    delegate?.openSelected(selected: selected, viewController: self)
    dismiss(animated: true, completion: nil)
}

代理设置正确。 TableView 显示不同行上的所有字符串。当我选择一行时,我希望当前 View 消失并显示带有所选字符串作为文本的标签。但是,该行已被选中,但没有任何反应,我可以继续使用 TableView 并选择新行,但什么也不会发生。

为什么解雇功能不起作用/实际上解雇了 VC?希望这是足够的信息。谢谢。

最佳答案

这是表格UITableViewDelegate的正确方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

还要确保在viewDidLoad

self.tableView.delegate = self

//

确认

class ViewController : UIViewController,UITableViewDelegate,UITableViewDataSource

关于ios - didSelectRowAtIndexPath 时关闭 View 并设置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50769746/

相关文章:

Swift:Set<Object> 基本操作会导致副本或指针?

ios - 无法识别的选择器发送到实例 _UIAlertControllerAlertPresentationController

objective-c - 查找某个div的xpath

ios - 使用 React Native 在 IOS 运行时出现 "Unhandled JS Exception: Native module cannot be null"错误

ios - 如何为每首歌曲添加一个 UILabel?

ios - NSData 到 Struct 搞乱数据

xcode - 在 Apple Watch 模拟器(xCode 8、Swift 3、iOS 10)上的 watchOS 中运行 SpriteKit 游戏 - libswiftSwiftOnoneSupport 错误

arrays - Swift 中的任意矩阵或数组大小

ios - AFnetworking 互联网可达性

iOS 8 - 如果您按下 iPad 上的主屏幕按钮并返回,键盘会改变颜色