I'm trying to send information between two Container Views
in Swift
, through a delegate and I keep getting unwrapping error when performing the protocol function.
topContainerViewController.swift
import UIKit
protocol topContainerDelegate{
func send(text:String)
}
class topContainerViewController: UIViewController {
var delegate: topContainerDelegate! = nil
@IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendMessage(sender: AnyObject) {
delegate!.send(textField.text!)
}
}
bottomContainerViewController.swift
import UIKit
class bottomContainerViewController: UIViewController, topContainerDelegate {
@IBOutlet var messageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func send(text: String) {
messageLabel.text = text
}
}
Question
How do I properly set up delegation between two container views?
I assume that you have both container views inside a View A, so check this out:
A more easy (and more convenient?) way would be that A would do the communication between your Container View B & C. This way, you don't have to go trough the whole app to set the delegate, which also should have a better performance in case your application is really big.
View A:
A is now the delegate of both B & C and inverse.
Let's assume that you want B to inform C about somehting:
View B:
Now you need to make A conform to the protocol of B "BChangedSomethingDelegate":
For C you would also have to implement a protocol which A will then conform to it. Worked on that solution and it's working pretty good.
extra tip: set your breakpoints to test if A,B and C are the correspondive delegates, during the breakpoints press "po xxx" directly in the logs while the app is stopped by breakpoint to check what xxx is.
Example: (po cDelegate) -> Nil or the delegate
DUDE You missed a step:
Thats cool..
But where have you specified the delegate in topContainerViewController 's instance would be instance of bottomContainerViewController ???
YOU MISSED ONE STEP: you haven't assigned object to delegate object of topContainerViewController 's instance SO YOU GOT ERROR It means you have not specified who responds to any call to delegate function's call from that object???? if you have not given who responds to it then its obvious you get error. since, i didn't find you have created instance of topContainerViewController, i guess you did by using segue.
when you create instances of topContainerViewController you need to specify delegate variable. If you go to topContainerViewController on button click from bottomContainerViewController then get the instance of bottomContainerViewController and assign self to delegate:
SOLUTION
if you do by segue: give identifier to segue here i gave "openTopContainerViewController" now Override prepareforsegue function in bottomContainerViewController
If you go to topContainerViewController by code: do like this: (keep like this code in action of button pressed or whenever you trigger to open the topContainerViewController)
HOPE YOU GOT IT :)
HERE IS THE PROBLEM . YOU DIDN'T ASSIGN ANYTHING TO delegate variable so It became always nil to avoid it you need to assign like above
The problem with your code is:
self
in yourBottomContainerViewController
.TopContainerViewController
you set your delegates initial value to nil, just leave it as an optional and use a guard to unwrap it.This works in my test application:
If you have any further questions feel free to download my working project and test it out for yourself.
Download Working Example
check whehter value of delegate nil!
change