I am trying to transfer data from one view manager to another using the tab bar controller. I have implemented my own Control Barbar 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 SecondViewControllerwith a variable logView. In my class SecondViewController, I have an array setting logthat will contain the value passed from my class CustomTabBarControllerClass. Here is my code for mine 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()
println(log!)
}
}
In my function, viewDidLoad()I am trying to print a log to the console using println(log!). When this code is run, the following error: fatal error: unexpectedly found nil while unwrapping an Optional value. So, how could I transfer data between two view managers?
didSelectViewController , .
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
var logView = self.viewControllers![0] as SecondViewController
logView.log?.append("Testing 123")
}