Answer the question
In order to leave comments, you need to log in
Where is my error in delegation?
I know that a similar question has already been asked by me , but I still don't understand what I'm doing wrong! I sat for about 2 days looking at all the possible lessons and explanations, in my head everything fits into these explanations, BUT it does not work! I hope you shed some light, I have no one else to turn to.
So the simplest example what I am trying to do is:
1) MainViewController
//Создаю протокол
protocol MainViewControllerDelegate {
func sayHello()
}
// в override
var delegate: MainViewControllerDelegate?
// пытался и в другом месте, не знаю, можно ли вызывать метод делегата из override
// вызываю
delegate?.sayHello()
var mainController = MainViewController()
override
: mainController.delegate = self
func sayHello() {
print("Hey!")
}
Answer the question
In order to leave comments, you need to log in
The error is most likely here:
var mainController = MainViewController()
Since you are using segue, I suspect that your controller is created in a storyboard. And the call MainViewController()
creates a new controller, which is not displayed anywhere.
You must set the delegate to prepare for segue in the class MainViewController
:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? SecondViewController {
self.delegate = destination
}
}
weak var delegate: MainViewControllerDelegate?
is it possible to call delegate method from override
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question