Transferring data between two view controllers using a custom panel control panel class

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()
        // 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 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")
    }
+4
1

SecondViewController, . viewControllers , (, , 1).

class ViewController: UIViewController {

    var log = [String]()

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        println(log)
    }
}

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

}
+7

All Articles