swift - 自定义委托(delegate)不会从 ContainerView 执行

标签 swift delegates

我尝试在我的 ViewController 中获取一个事件/操作,如果我的 Tableview 中的一行被按下。
我有以下 Storyboard: Project's Storyboard

我的 Storyboard 中的 Tableview 的 StoryboardID 为“myTableViewController”
我的(目的地)ViewController:

import UIKit

class ViewController: UIViewController, songsDelegate {

@IBOutlet weak var label: UILabel!

   override func viewDidLoad() {
       super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       self.view.backgroundColor = UIColor.red
       let storyboard = UIStoryboard(name: "Main", bundle: nil)
       let tableviewController = storyboard.instantiateViewController(withIdentifier: "myTableViewController") as! TableViewController
       tableviewController.delegate = self


   }

   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }

   func songTapped(_ title: String) {
      print(" action ")
      self.label.text = title
   }

}

我的(响应)TableViewControllerDelegate 协议(protocol):

import UIKit



protocol songsDelegate: class {
    func songTapped(_ title: String)
}



class TableViewController: UITableViewController {
    weak var delegate : songsDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 12
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "Titel \(indexPath.row + 1)"
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        self.delegate?.songTapped( "Titel \(indexPath.row + 1)" )
    }
}

我的问题是:为什么 ViewController 中的 func songTapped(_ title: String) 没有执行?

最佳答案

您正在实例化一个 TableViewController 对象,但您没有将它存储到强引用中,也没有将它的 View 作为 subview 添加到 ViewController 的 View 上

   let tableviewController = storyboard.instantiateViewController(withIdentifier: "myTableViewController") as! TableViewController
   tableviewController.delegate = self

当viewDidLoad函数结束时,你的变量tableviewController就消失了

关于swift - 自定义委托(delegate)不会从 ContainerView 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496203/

相关文章:

ios - 点击 super View 中嵌入的自定义 UIView 中未收到的手势识别器

jquery - 是否可以在 jquery 的委托(delegate)函数中添加多个选择器?

jquery - 有没有更好的方法在 jQuery Mobile 中附加到 pageshow?

swift - 子类化委托(delegate)的正确方法是什么?

swift - 多个 UIPickerView 在 Swift 中以编程方式选择第一行

ios - 在 Swift 上弹出 ViewController

ios - 具有配置文件和基本游戏化功能的 iOS 应用程序的 Firebase 数据结构

ios - Swift 4 UIActivityIndi​​catorView 不以 UITableView 为中心

ios - 为自定义 UINavigationBar 设置委托(delegate)

iphone - 打开和关闭其他 UIViewController - 除了使用协议(protocol)和委托(delegate)之外的任何其他方法?