zyxwvu Shi
zyxwvu Shi

Reputation: 31

Reloading data when clicking back bar button of an UINavigationController

I have an UINavigationController and it has an UITableViewController as its root controller. I reload the data for the table view in viewWillAppear. When I select an item in the table view it will push an new UIViewController with which the user could edit the data that will be shown in the table view, and then I clicked Back of the navigation, viewWillAppear of the UITableViewController was just not called and the data was not reloaded.

So in this case where could I reload the data for the table view?

Thanks.

Upvotes: 0

Views: 799

Answers (2)

Mika
Mika

Reputation: 5845

viewWillAppear is called when you hit the back button. Just put a log in there if you don't believe me! Most likely the table is not reloading the way you want it to because there is something wrong with the table datasource.

Upvotes: 0

Nikita Zernov
Nikita Zernov

Reputation: 5635

The easiest way to do that is to use UINavigationControllerDelegate. There are two methods available:

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated animated: Bool)
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated animated: Bool)

In your UITableViewController(root) you should add UINavigationControllerDelegate and method navigationControllerWillShowViewController will be called when you are going back. Here's an example:

class RootTableViewController: UITableViewController, UINavigationControllerDelegate {
    ...
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated animated: Bool) {
        //Will run  
    }
    ...
}

Upvotes: 1

Related Questions