protocol useForDist {
func findpindist()
}
class ViewController1: UIViewController, MKMapViewDelegate, useForDist {
override func viewDidLoad() {
super.viewDidLoad()
}
func findpindist() {
print("Test")
}
}
class HomePageController: UIViewController {
var delegate: useForDist?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate?.findpindist()
}
}
Why is this not printing Test? Am I missing an aspect? I do not want a segue to a different view controller or a pop-up of one or a displaying. I’m new to XCode/Swift so if this is easily solvable bear with me 🙂
2
Answers
Alright I have a couple of suggestions for you!! First off, I would change your function to have public on the front of it. So it would look something like this:
Put is outside of your viewcontroller class and you should be able to call it in homePageViewController by putting
findpindist()
in the viewDidLoad.However, this looks sloppy and is very buggy so I have a better solution for you! There is something called NotificationCenter. This will allow you to pass data between viewcontrollers without a segue or anything. To use this, first put this code inside the viewDidLoad of the homePageController
Next, add this code to your viewController1. the observer stays in the viewDidLoad and the objective c function goes outside of the viewDidLoad
As long as you have your viewController1 loaded, it should print the statement. you can use this code to send any data between viewControllers. This should help with what I think you are trying to achieve, but as the other person said, I’m not sure what your objective is.
heres some additional resources:
Posting Messages with NotificationCenter
A deep walkthrough of NotificationCenter
You forgot a few things.
first, you need to access to second ViewController
ViewController
from the first ViewControllerHomeViewController
.Second, you need to set the
HomeViewController
delegate.