Ali_bean
Ali_bean

Reputation: 341

Sending data between tab bars

i need to send a piece of data indicating which row sent it from one of my tab tableviewcontroller to main view controller, then I can take action based on the value in viewDidAppear

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //selectedRow = indexPath.row
    self.tabBarController?.selectedIndex = 0
}

and then do something in the other view controller:

override func viewDidAppear(animated: Bool) {
    //selectedRow = row sent from tab bar,
    //load data from plist at index of the selected row
}

Whats the best way to do this? Thanks

Upvotes: 0

Views: 509

Answers (2)

imnosov
imnosov

Reputation: 856

You can declare property for your other view controller and set its value in tableView:didSelectRowAtIndexPath: method.

OtherViewController:

class OtherViewController: UIViewController {
    var selectedRow: Int = 0

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // load data from plist at index using selectedRow property
    }
}

TableViewController's tableView:didSelectRowAtIndexPath::

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var tabIndex = 0

    if let vc = self.tabBarController?.viewControllers?[tabIndex] as? OtherViewController {
        vc.selectedRow = indexPath.row
    }
    self.tabBarController?.selectedIndex = tabIndex
}

Upvotes: 2

Gal Marom
Gal Marom

Reputation: 8629

You should use prepareForSegue and then you will be able to transfer information between the segues. Take a look at the identifier property and the destination one

Upvotes: 0

Related Questions