A viewcontrollerB is the root view of nav controller and first view of tab view controller at same time. How to pass a data from initial viewA through nav and tab controller to ViewB ? Thanks
Asked
Active
Viewed 378 times
1 Answers
0
Give your tab bar controller a custom class, for example MyTabBarController, and declare a variable there that will receive the text from your textfield in log in view controller (via prepareForSegue). then you will be able to access that variable from your home view controller.
In your log in view controller:
@IBAction func ButtonPressed(sender: AnyObject) {
performSegueWithIdentifier("toTabBarController", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let tabBarController = segue.destinationViewController as? MyTabBarController{
tabBarController.someVariable = someTextField.text!
}
}
in your tab bar controller:
var someVariable = String()
in you home tableview controller:
override func viewWillAppear(animated: Bool) {
let tabBarController = self.tabBarController as! MyTabBarController
someVariable = tabBarController.someVariable
print("the text is",someVariable)
}

valencieu
- 178
- 1
- 12
-
Hi, thanks. I have tried your code on my app. The problem is that my viewB is currently embedded in both nav controller and tab controller. So the value can not pass directly from logView to viewB by send value to tab controller – Janice Zhan May 04 '16 at 05:04
-
Yes, I looked at your storyboard picture, recreated the project and could make it work. Please give me more information so I can help you. Can you successfully pass your variable from logView to your tabbar controller using performSegueWithIdentifier? – valencieu May 04 '16 at 05:12
-
if I only embedded my view on tab controller, it can pass data successfully. However, add one more Nav controller, it does not work – Janice Zhan May 04 '16 at 05:16
-
how are you trying to get your variable in your viewB? You can edit your question and add the following: Code used in view controller A, in your tabbar controller, and in your view controller B. Also show any error you've got while trying your code, so I can understand better and help you. – valencieu May 04 '16 at 05:58
-
I tried your answer again, and it works. Thanks a lot – Janice Zhan May 04 '16 at 10:02