When I open a custom UIView from a UITableViewController, how do I close the custom view and go back to the table view?

StackOverflow https://stackoverflow.com/questions/1029703

Question

I'm using a UITableViewController for a menu in a game. The table view opens a a view controller for my custom UIView that shows the game. When the game finishes the UIView is notified (which is kinda ruining the MVC principals) and from there I am kinda lost.

Questions:

  1. Can a UIView communicate with its controller? How?
  2. Can one controller talk to the one that started it? How?
  3. How do I transition between all of this complicated web of views and controllers gracefully?
Was it helpful?

Solution

  1. Use a delegate protocol here. Your custom game UIView can use a delegate property and call methods on this delegate when events happen (game over, game paused, view closed, etc). Here's a great post on using delegates: How do I create delegates in Objective-C?

  2. I'd recommend using a UINavigationController. You don't necessarily need to show the navigation bar, but if you nest your view controllers in a navigation controller you have access to -pushViewControllerAnimated: and -popViewControllerAnimated: which make it really easy to navigation between levels of nested view controllers.

  3. Another benefit of UINavigationController - you'll get a nice slide animation when you switch between views.

There are other 3rd party mechanisms out there that you may prefer over the UIKit UINavigationController/UIViewController mechanisms. Check out the Three20 project, in particular the TTNavigationCenter class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top