-3

I want to use a delegate method to update some UI in my top view controller from the current view controller. I'm getting error unexpectedly found nil while unwrapping an Optional value. Please have a look at the following code and suggest what's wrong.

First View Controller

Second View Controller

Vakas
  • 5,382
  • 3
  • 33
  • 44

2 Answers2

0

I fixed it. I was missing prepareForSegue implementation in my first View Controller.

I had to assign the delegate to self in prepareForSegue. I can't do it like

let vc = SecondViewController()
vc.sampleProtocol = self

which I was doing in my viewDidLoad()

Vakas
  • 5,382
  • 3
  • 33
  • 44
0

You cannot set the delegate like you're doing in your code in viewDidLoad. You have to implement prepareForSegue and set your delegate there.

Nick
  • 21
  • 4