I am attempting to pass data from one viewcontroller to another using a Tab Bar Controller. I have implemented a custom Tab Bar Controller class. Here is my code for this class:
class CustomTabBarControllerClass: UITabBarController, UITabBarControllerDelegate {
override func awakeFromNib() {
self.delegate = self;
}
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
var logView = SecondViewController()
logView.log.append("Testing 123")
}
}
As you can see in my code, I am creating an instance of SecondViewController
with the logView
variable. In my SecondViewController
class, I have a log
array setup that will hold the value being passed from my CustomTabBarControllerClass
class. Here is my code for my SecondViewController
.
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var log = [String]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return log!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("logCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = log![indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println(log!) //fatal error: unexpectedly found nil while unwrapping an Optional value
}
}
In my viewDidLoad()
function, I am attempting to print the log to the console with println(log!)
. When this code runs, I am presented with following error: fatal error: unexpectedly found nil while unwrapping an Optional value
. So how would I go about passing data between the two viewcontrollers?
Update
The didSelectViewController
function has been updated with the code below, however, I am still receiving the same error message.
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
var logView = self.viewControllers![0] as SecondViewController
logView.log?.append("Testing 123")
}
Your tab bar controller already has an instance of SecondViewController, so you shouldn't be instantiating a new one. Use the tab bar controller's viewControllers property to access the one you want (presumably, from the name, the one at index 1).
class ViewController: UIViewController {
var log = [String]()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
println(log)
}
}
In the tab bar controller,
class RDTabBarController: UITabBarController , UITabBarControllerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
var logView = self.viewControllers![1] as ViewController
logView.log.append("Testing 123")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With