user2722667
user2722667

Reputation: 8651

Swift protocol delegate not being fired

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

Upvotes: 2

Views: 406

Answers (3)

fdiaz
fdiaz

Reputation: 2600

  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!")
    }
}

Upvotes: 1

Arsen
Arsen

Reputation: 10951

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

Upvotes: 0

Lokesh Dudhat
Lokesh Dudhat

Reputation: 449

please define delegate value in view didload:

delegate = self

Upvotes: 0

Related Questions