When I open a custom UIView from a UITableViewController, how do I close the custom view and go back to the table view?
-
06-07-2019 - |
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:
- Can a
UIView
communicate with its controller? How? - Can one controller talk to the one that started it? How?
- How do I transition between all of this complicated web of views and controllers gracefully?
Solution
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?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.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.