1

I've to get value from my JSON and pass this string to a variable in the second View. So, my problem it's not get value from JSON because I did it. How I can do it. I've just tried prepare (for segue) and other code but any of thats worked. Do you have an advice for me?

I hope I have been clearer. Sorry. 1) First 2) Second

  • post how you navigate push /segue/present and how you send the data ? – Shehata Gamal Dec 06 '18 at 14:28
  • @Sh_Khan I cancelled all because doesn't work –  Dec 06 '18 at 14:30
  • use prepare for segue – canister_exister Dec 06 '18 at 14:45
  • @canister_exister Just tried. self.titolo = list["title"] as! String self.descrizione = list["tagline"] as! String self.performSegue(withIdentifier: "Home", sender: self) override func prepare(for segue: UIStoryboardSegue!, sender: Any?) { var vc = segue.destination as! Home vc.nomeatt = self.titolo! } –  Dec 06 '18 at 14:56
  • vc.nomeatt??? what is that? – canister_exister Dec 06 '18 at 14:59
  • @canister_exister Yeah sorry. nomeatt = title so: vc.title = self.title! –  Dec 06 '18 at 15:01
  • class SecondView: UIViewController or class Home: UIViewController ? – canister_exister Dec 06 '18 at 15:06
  • @canister_exister in the First View –  Dec 06 '18 at 15:19
  • You have problem with your class name – canister_exister Dec 06 '18 at 15:22
  • @canister_exister wait now i explain you all. I confused you –  Dec 06 '18 at 15:28
  • What about to add code when you try to switch to another VC and your prepare for segue? – canister_exister Dec 06 '18 at 15:46
  • @canister_exister like that: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.destination is Home { let vc = segue.destination as? Home vc?.titleHome = "Test" } } –  Dec 06 '18 at 16:04
  • @canister_exister ok i did some test and now i know why it's not working. But i don't know how to resolve it. The problem is that betwen LoginController and Home Controller i have a Navigation Controller. file:///Users/stefanoabbruzzese/Desktop/Schermata%202018-12-06%20alle%2017.16.58%20(2).png –  Dec 06 '18 at 16:22
  • There are tons of tutorials for this online, you may take a look on this post- https://stackoverflow.com/a/53540610/5758873 – Niv Dec 06 '18 at 18:49

1 Answers1

0

This should do it.

let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "Your Home View Controller identifier") as! SecondView
homeViewController.title = self.title
homeViewController.description = self.description
self.navigationController?.pushViewController(homeViewController, animated: true)
Vlad M.
  • 139
  • 4
  • 1
    His problem that he is trying to pass data to `SecondView` view controller but he use wrong class name `Home` – canister_exister Dec 06 '18 at 15:17
  • If you create a segue from first to second and use prepare for segue to set description and title, that should work. Note: why do you make them IUO and not pure optional ? – claude31 Dec 06 '18 at 15:47
  • @VladM. Look for my last comment for canister –  Dec 06 '18 at 16:23