dyatesupnorth
dyatesupnorth

Reputation: 830

How to access the master's tableview from detail view in iOS

A portion of my app has an embedded master-detail section. Each detail view is using a custom UIViewController. When I change the value of something inside one of these UIViewControllers I need to be able to grey out one of the table rows in the master UITableViewController.

The closest I have seen to a solution is to use NSNotificationCenter to bubble up any changes, though this feels a little untidy..

Another solution is to use delegates? But I haven't come across any example solutions or tutorials in how to use this in Swift?

I've also experimented just trying to access the table view by navigating back up the hierarchy:

   let navController = self.splitViewController!.viewControllers[0];

   navController.tableView.reloadData()

I know the example above is wrong, but I don't know how to access the master view that way, or even if it is the right approach.

Oh, I am trying to call reloadData() because in the master view there is some logic which checks the condition as to wether to grey out a table row is applicable (i'm using Core Data)

Upvotes: 1

Views: 2063

Answers (3)

joern
joern

Reputation: 27620

I've seen that you figured this one out already. However a cleaner and more future proof way would be to use a delegate protocol:

protocol DetailViewControllerDelegate: class {
    func reloadTableView()
}

Then add a delegate property to your DetailViewController class and implement the call to the delegate:

class DetailViewController: UIViewController {
    weak var delegate: DetailViewControllerDelegate?
    ....

    func reloadMasterTableView() {
        delegate?.reloadTableView()
    }
}

And then in your MainViewController implement the delegate method:

extension MainViewController: DetailViewControllerDelegate {
    func reloadTableView() {
        tableView.reloadData()
    }
}

Don't forget to set the delegate on your DetailViewController instances when you create them:

let detailViewController = DetailViewController()
detailViewController.delegate = self

Upvotes: 4

Imran
Imran

Reputation: 2941

I would suggest you use NSNotificationCenter . If you want to to do it via Navigation controller here is to code should work for you in swift.

let navController: UINavigationController = self.splitViewController!.viewControllers[0] as! UINavigationController

let controller: MasterViewController = navController.topViewController as! MasterViewController
controller.tableView.reloadData()

Upvotes: 3

dyatesupnorth
dyatesupnorth

Reputation: 830

Since I was able to access my viewController, I was able to access the parent viewcontroller like so:

 func reloadMasterTableView(){
        let navVC: UINavigationController =  self.splitViewController!.viewControllers[0] as! UINavigationController


        let sectionsVC : UIMasterViewController = navVC.topViewController as! UIMasterViewController
        sectionsVC.tableView.reloadData()

    }

Upvotes: 2

Related Questions