views:

1152

answers:

1

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?
+2  A: 
  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.

pix0r
Hmmm, I think I need something a bit deeper than that to actually understand why UIView doesn't have a parent / controller field, and how to do this **right**, not pretty.
Mikle
I think the delegate idea is the right one for the first question, and it's the most common way of passing messages out of a UIView without breaking MVC too badly. The thought of a navigation controller is also good - a navigation controller is basically a stack of view controllers, so each controller has a sense of its "parent" controller. This answer seems to fit pretty well, both in the sense of doing things "right" and doing things "pretty." +1
Tim
Furthermore, a UIView doesn't have a parent/controller field because a UIView doesn't necessarily have to have a controller. It may have a superview, which is a property of the view object.
Tim
UIViewController does have a parentViewController property, but that's only useful with UINavigationController or similar UIKit controllers. Note, I added a link to the Three20 project - you may find TTNavigationCenter solves some of your problems.
pix0r
Tim's second comment was the answer I was looking for. Thanks :)
Mikle