Hi!
I have protocol AllNotebookDelegate:
protocol AllNotebookDelegate: class {
func passData(text: String,nameOfNotebook: String)
}
And also I have class AllNotebookCollectionVC with collectionView(didSelectRowAt:) method:
class AllNotebookCollectionViewController: UICollectionViewController {
//MARK: - Variables
var notebookItems: [NotebookItem] = []
weak var delegate: AllNotebookDelegate?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedItem = notebookItems[indexPath.item]
delegate?.passData(text: selectedItem.text, nameOfNotebook: selectedItem.titleOfNotebook)
// Present EditorVC
let editorVC = storyboard!.instantiateViewController(withIdentifier: "editorVC")
let navigationController = UINavigationController(rootViewController: editorVC)
present(navigationController, animated: true, completion: nil)
}
}
This is editorVC:
class EditorViewController: UIViewController {
//MARK: - Variables & Outlets
@IBOutlet weak var textView: UITextView!
//MARK: - Private methods
override func viewDidLoad() {
super.viewDidLoad()
let allNotebookVC = storyboard?.instantiateViewController(withIdentifier: "allNotebookVC") as! AllNotebookCollectionViewController
textView.text = ""
allNotebookVC.delegate = self
}
extension EditorViewController: AllNotebookDelegate {
func passData(text: String, nameOfNotebook: String) {
titleOfNavigationBar = nameOfNotebook
textView.text = nameOfNotebook
}
When I add a breakpoint at collectionView(cellForRowAt:) in AllNotebookVC debugger show me this:
collectionView | UICollectionView | 0x00007fee26878c00 | |
indexPath | IndexPath | 2 indices | |
self | Scroll.AllNotebookCollectionViewController | 0x00007fee25c09b00 | |
UIKit.UICollectionViewController | UICollectionViewController | ||
notebookItems | [Scroll.NotebookItem] | 1 value | |
delegate | AllNotebookDelegate? | nil | none |
selectedItem | Scroll.NotebookItem | 0x0000600003add100 | |
titleOfNotebook | String | "" | |
nameOfImageOfNotebook | String | "wallper" | |
text | String | "" | |
editorVC | Scroll.EditorViewController | 0x00007fee25e0f580 | |
UIKit.UIViewController | UIViewController | ||
textView | UITextView? | nil | none |
titleOfNavigationBar | String | "" | |
navigationController | UINavigationController | 0x00007fee27015200 | |
UIKit.UIViewController | UIViewController |
So, why delegate = nil?