0

I am segueing from a view controller to a tabbarcontroller and I want to pass data. I want to pass "sendAuthor" to the first view controller of the tabbarcontroller.

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "FirstVC" { let vc = (segue.destination as? UITabBarController)?.viewControllers?.first as? CommentProfileViewController vc?.sendAuthor = sendAuthor! vc?.Username.text = sendAuthor! print(sendAuthor!) } } 

In order to make sure I am getting the right thing, I included a print statement print(sendAuthor!) and I am getting the correct print statement. I also included a print statement in the destination view controller but the print statement returns nil

var sendAuthor: String? override func viewDidLoad() { super.viewDidLoad() print(self.sendAuthor) } 
13
  • @Sh_Khan the vc itself Commented Jun 27, 2018 at 19:57
  • just before let vc = , add this: print(type(of: (segue.destination as? UITabBarController)?.viewControllers?.first)) and let us know what it prints. Commented Jun 27, 2018 at 20:01
  • Do you hook segue destination to the tabBar itself or to the first Vc of it , Also are you sure that the first VC name is CommentProfileViewController Commented Jun 27, 2018 at 20:02
  • @vacawama it prints out Optional<UIViewController> Commented Jun 27, 2018 at 20:03
  • @Sh_Khan I hooked it to the tabbar itself Commented Jun 27, 2018 at 20:04

1 Answer 1

1

Your VC is imbedded in a UINavigationController. You need to take that into account as well:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "FirstVC" { guard let tabbar = segue.destination as? UITabBarController, let navcon = tabbar.viewControllers?.first as? UINavigationController, let vc = navcon.topViewController as? CommentProfileViewController else { return } vc.sendAuthor = sendAuthor! vc.Username.text = sendAuthor! print(sendAuthor!) } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.