0

I try get a event/action in my ViewController, if is a row in my Tableview ist pressed.
I have following storyboard: Project's Storyboard

the Tableview in my Storyboard has the StoryboardID "myTableViewController"
My (destination) 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
   }

}

my (response) TableViewController with Delegate 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)" )
    }
}

My Question is: Why will func songTapped(_ title: String) in ViewController not executed ?

swiftUser
  • 341
  • 6
  • 14
  • 1
    See: https://stackoverflow.com/questions/31975942/swift-delegate-embedded-view-controller-and-parent – dan Mar 26 '18 at 16:31

1 Answers1

1

You are instantiating a TableViewController object but you don't store it into a strong reference, neither you add its view as a subview on ViewController's view

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

When the viewDidLoad function ends, your variable tableviewController disappears

omarzl
  • 589
  • 4
  • 9