1

My question is almost identical to the one found here : How to call viewDidLoad after [self dismissModalViewControllerAnimated:YES];

and I'm pretty sure the answer is exactly what I need too. I have a ViewController (let's call it A) has a list of tasks. When you click on a task in the table view in A, it brings up another ViewController (B) modally. When I'm done with the task on B, I want to go back to A by calling dismissViewController, but how do I reload the table?

The table is being fed data from a REST API so I don't think just deleting that row is going to work. I am going to need to call the REST API to get the list of tasks and then reload the table view.

I am not very knowledgable in Obj C so I'm having trouble translating this. This is what I think needs to be done.

Make protocol for B ViewController delegate. Make A ViewController a delegate of BViewController and fill out that protocol.

My question is how does B ViewController and B ViewController Delegate link up? In the B ViewController is there a way to say that this action happened so that A knows it did?

Community
  • 1
  • 1
seongju
  • 357
  • 1
  • 4
  • 15
  • I would use either NSNotificationCenter or a delegate to relay back to the presenting view controller your intention. There is also no reason to reload the table every time you dismiss the modal, that is a lot of network calls that you don't need to do. A better approach would be to use one of the methods I mentioned and modify the presenting view controllers data source appropriately then reload the table view.....Also viewWillAppear is NOT called when a modal view controller is dismissed to address the other answers. – DBoyer Jul 28 '15 at 19:09
  • I think the other answerers are saying that I should override viewWillAppear in A ViewController. I agree with you on your other point because the network calls are pretty expensive (sometimes 95 kb of JSON). So do you think I should delete the row that I don't need in the data source and then reload the table again? – seongju Jul 28 '15 at 19:16
  • 1
    Yes, have the modal view controller tell the presenting view controller to delete whichever row through a delegate then either reload the table view or just delete that single row. – DBoyer Jul 28 '15 at 19:18

2 Answers2

2

What you can try is

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tableView.reloadData() //or whatever your reload data function is
}
pbush25
  • 5,048
  • 2
  • 23
  • 33
2

Many ways to go about this, but the simplest one is probably to override viewWillAppear in your tableViewController and place the reloadData call in there.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}

viewWillAppear is called every time the viewController is displayed on the screen, so whenever you segue back to your tableViewController, it will be called and your objects will be reloaded.

Kelvin Lau
  • 5,773
  • 3
  • 28
  • 49
  • Is it considered "bad practice to do this?" – seongju Jul 28 '15 at 19:08
  • It can be considered bad practice is using `reloadData`. It's short and easy to use to reload the tableView's data, but every time `reloadData` is called, the `TableView` does 2 things: reloads the tableView with the dataSource, and also resets the scroll position to all the way up top. If that does not affect your user experience, then it's fine. But if the user is expected to continue scrolling down your tableView after segueing back, then you just made the user jump back to the top of the screen, forcing him/her to scroll back to the original position. – Kelvin Lau Jul 28 '15 at 19:13
  • As for reloading the data, `viewWillAppear` is a suitable place. – Kelvin Lau Jul 28 '15 at 19:14