4

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")
    }
three3
  • 2,756
  • 14
  • 57
  • 85

1 Answers1

7

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")
    }

}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thank you for this info. I have tried doing what you said and I still received the `fatal error: unexpectedly found nil while unwrapping an Optional value` error message. Is there something I need to change in my `SecondViewController` or was the code in that file okay? I have updated my original posting to reflect the code changes. – three3 Nov 25 '14 at 23:09
  • @three3, you still need to instantiate your array, log. You created the variable, but that doesn't create the array. You should create it in the controller's init method since then it will be available when you try to set its value. – rdelmar Nov 25 '14 at 23:12
  • I have updated my code in `SecondViewController` to be `var log = [String]()`. However, the app just crashes now when I click on a tab and it does not give me any error messages. I must be intializing it incorrectly. Do you mind providing the code required to do this? Thank you very much for your help so far! – three3 Nov 25 '14 at 23:18
  • 1
    @three3, that code is correct. viewDidLoad might be too early to see the value. Try moving that code to viewDidAppear. Also, is SecondViewController in the first tab or the second? – rdelmar Nov 25 '14 at 23:26
  • `SecondViewController` is the second tab. Also, I tried moving the `println(log)` to the `viewDidAppear` function in my `SecondViewController`, but the app still crashes. Any other thoughts? – three3 Nov 25 '14 at 23:29
  • @three3, if its in the second tab, then you should have viewControllers[1], not 0 as you posted. Also you shouldn't have the "!" after log, because log is not an optional. – rdelmar Nov 25 '14 at 23:31