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.
Asked
Active
Viewed 393 times
-3

Vakas
- 6,291
- 3
- 35
- 47
-
Whether your delegate method called? – Kathiravan G Jul 07 '15 at 08:31
-
It's throwing error at sampleProtocol!.updateTextLabel(someText) – Vakas Jul 07 '15 at 08:33
-
Check some text is valid string or nill – Kathiravan G Jul 07 '15 at 08:41
-
I've printed it's value and it is a valid string. – Vakas Jul 07 '15 at 08:43
-
Refer this http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values – Kathiravan G Jul 07 '15 at 08:44
-
Can you show how you present your `SecondViewController` ? – Michal Jul 07 '15 at 08:47
2 Answers
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
- 6,291
- 3
- 35
- 47
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