To transfer data between controllers programmatically?

There is HomeViewController and NextViewController how to pass variable from the first to the second without using storyboard and segue ( and back from the second to the first), the variable needs to be passed to download the viewDidLoad()
The transition is so
func handleSearch(){
 navigationController?.pushViewController(AlfavitController(), animated: true)
 }

User Default, Core Data does not offer.
If possible then with example and not just a response, like, (use the delegates, read about the singleton)
FOR EARLIER THANK YOU TO EVERYONE WHO HELPS!!!
July 2nd 19 at 16:39
4 answers
July 2nd 19 at 16:41
Solution
If you are using storyboard, then to pass data through prepareForSegue

Let's say you have NextViewController variable name: String
class NextViewController: your uiviewcontroller {
 var name: String!
}

you need to pass the name, then in prepareForSegue there are two ways to detecting the transition to the NextViewController:

1. If we have only one transition to the NextViewController or a lot of them with different buttons, but the data is always the same
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if let dest = segue.destination as? NextViewController {
 dest.name = "Jessica"
}
}


2. If we have different transitions in NextViewController and the set of transmitted data varies
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if let dest = segue.destination as? NextViewController {
 if segue.identifier == "action1" {
 dest.name = "Jessica"
 } else if segue.identifier == "action2" {
 dest.name = "Jessica"
 dest.sex = "female"
}
}
}


If you programmatically create a transition, everything else easier. Again two options how to reach NextViewController:

1. It is on the storyboard
let dest = storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController


2. Or is he not on the storyboard...
let dest = NextViewController(nibName:"NextViewController", bundle: nil)


Then attach the data

dest.name = "Jessica"

And two options how to open NextViewController:

1. In the presence of a navigationController'
navigationController?.pushViewController(dest, animated: true)


2. Simple modal window
dest.modalPresentationStyle = .fullScreen
dest.modalTransitionStyle = .coverVertical
present(dest, animated: true, completion: nil)


To back from the second to the first data transfer, you can use a delegate or notifications.

If delegates
protocol NextViewControllerDelegate {
 func callback(_ someString: String)
}

class NextViewController: your uiviewcontroller {
 var name: String!
 var delegate: NextViewControllerDelegate?

 func someMethod() {
 delegate?. callback("delegate callback")
}
}

//Here is the controller from which open CustomViewController, in addition to the name we now need to set delegate=self

class HomeViewController: your uiviewcontroller, NextViewControllerDelegate {
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if let dest = segue.destination as? NextViewController {
 dest.name = "Jessica"
 dest.delegate = self
}
}

 //MARK: NextViewControllerDelegate
 func callback(_ someString: String) {
 print("got the callback with the string: \(someString)")
}
}


If notifications
let kNotifNextViewControllerCallback = NSNotification.Name(rawValue: "kNotifNextViewControllerCallback")

class NextViewController: your uiviewcontroller {
 var name: String!

 func someMethod() {
 NotificationCenter.default.post(name: kNotifNextViewControllerCallback, object: "notification text")
}
}

//Here HomeViewController which opened CustomViewController, we will listen kNotifNextViewControllerCallback method gotNotification

class HomeViewController: your uiviewcontroller {
 override func viewDidLoad() {
super.viewDidLoad()
 NotificationCenter.default.addObserver(self, selector: #selector(gotNotification(notification:)), name: kNotifNextViewControllerCallback, object: nil)
}

 deinit {
NotificationCenter.default.removeObserver(self)
}

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if let dest = segue.destination as? NextViewController {
 dest.name = "Jessica"
}
}

 //MARK: Notification observer
 func gotNotification(notification: Notification) {
 print("got notification with object: \(notification.object)")
}
}
July 2nd 19 at 16:43
Solution
Magic:
func handleSearch() {
 navigationController?.pushViewController(AlfavitController(with Magic), animated: true)
}


UPD. Very funny joke

UPD2.
1. Take a view controller that has some kind of dependency (read as "the variable that he needs to pass")
2. Create an initializer with a parameter in the form of this dependence
3. Create a controller with this initializator
4. Pushin it
5. ???
6. PROFIT
I do not understand? Supposedly the trick - abagail_Jaskolski commented on July 2nd 19 at 16:46
I still do not understand. - abagail_Jaskolski commented on July 2nd 19 at 16:49
In the end, how to pass variable then? - abagail_Jaskolski commented on July 2nd 19 at 16:52
Friend well, tell me an example what and why. Read about initializers and that nothing is given. - abagail_Jaskolski commented on July 2nd 19 at 16:55
Just structure with the old man do not want to use if there are still other possibilities - abagail_Jaskolski commented on July 2nd 19 at 16:58
: wrote the same - create an initializer with a parameter(i.e. init(param:) ); create controller using this the initializer.; pushim created. - pasquale commented on July 2nd 19 at 17:01
July 2nd 19 at 16:45
Solution
Found 1 solution using the structure with static variable
struct Manager {
static var id = String()
}
ARE THERE ANY OTHER SOLUTIONS???
July 2nd 19 at 16:47
Solution
Because viewDidLoad is called when you request a download the film view from the controller (even if it never showed) that:
  1. Controller prescribed propertylet myValue : someType?
  2. Instantiates controller
    let myVC = myStoryboard.instantiateViewController(withIdentifier: "myVC") as! myVC

  3. Setupis the controller propertymyVC.myValue = someValue
  4. Pushes controller
  5. ....Profit
Now it is clear THANK you!!! Just maybe you initially get your answers seem clear and simple BUT it is not))) - abagail_Jaskolski commented on July 2nd 19 at 16:50
I described another option, not one that is described - abagail_Jaskolski commented on July 2nd 19 at 16:53

Find more questions by tags SwiftiOS