2

I have a VC that contains a containerview that holds a table view. = Root VC holds the tableview

I have setup a delegate from the tableview to the root VC that checks if the screen is being scrolled up or down, and this works.

What I now want to do is to have a delegate between root VC to the table view. When a button is clicked in the root VC I want to fire a function in the table view.

This will make the viewcontrollers have delegates implemented on each other - is that a problem?

eg:

class RootVC, tableViewDelegate
class Tableview, RootVCDelegate

My delegate looks like this:

protocol RootVCDelegate: class {
    func RootVCDidTouchGrid(controller: RootViewController)
}

class rootvc { ...

weak var delegate: RootVCDelegate?

@IBAction func gridButtonDidTouch(sender: AnyObject) {
        delegate?.RootVCDidTouchGrid(self)
    }

Then in table view:

class tableview, RootVCDelegate {..

func RootVCDidTouchGrid(controller: RootViewController) {
        println("touched!")
    }

So why is never println("touched!") fired?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2722667
  • 8,195
  • 14
  • 52
  • 100

3 Answers3

1
  1. You created the (weak) delegate property of type RootVCDelegate in your RootViewController
  2. Your TableView class conforms to RootVCDelegate
  3. You forgot to assign the delegate property of RootViewController to your TableView instance.

You're missing something like:

class tableview, RootVCDelegate {..

    override func viewDidLoad() {
        super.viewDidLoad()
        rootViewController.delegate = self
    }

    func RootVCDidTouchGrid(controller: RootViewController) {
        println("touched!")
    }
}
fdiaz
  • 2,600
  • 21
  • 27
  • If I try that it says rootViewController does not have a member named delegate. I have done the exact same implementation in other places and it works, thats strange – user2722667 Sep 12 '15 at 22:22
  • My guess is that it has something to do with tableview being inside a container view in RootViewController? In RootViewController I am using the segue segue.identifier == "TableSegue". Could I pass the function there? – user2722667 Sep 12 '15 at 22:26
  • Not sure really, in your first post it's clear that rootVC does have a member named delegate. class rootvc { ... weak var delegate: RootVCDelegate? ... – fdiaz Sep 12 '15 at 22:44
  • But in any case, if RootVC owns the Container, and the Container owns the TableView, then it's probably better for RootVC to tell the Container to tell the TableView that the grid was touched instead of using delegation. – fdiaz Sep 12 '15 at 22:45
  • Thanks, but how can I do that, since the embedded container view doesnt have a class/vc? The segue gets fired from RootVc – user2722667 Sep 12 '15 at 22:52
  • Can you explain what's your hierarchy? I'm a bit lost. So you have something like RootVC that pushes a TableVC? If that's the case then you can just hold a reference to the TableVC in your RootVC and call the method in TableVC directly when you need to. – fdiaz Sep 12 '15 at 23:06
0

please define delegate value in view didload:

delegate = self
Lokesh Dudhat
  • 449
  • 3
  • 10
0

It looks like you forgot to set a delegate. Just set it delegate = self in viewDidLoad

Arsen
  • 10,815
  • 2
  • 34
  • 46