2

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?
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Mikle
  • 1,842
  • 4
  • 20
  • 32
  • What I did in the end: Create a "game controller" with the all the game specific actions. Create a view controller that inherits from the game controller. After initialization set the view's parent to the view controller. – Mikle Jun 27 '09 at 13:11

1 Answers1

2
  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.

Community
  • 1
  • 1
pix0r
  • 31,139
  • 18
  • 86
  • 102
  • 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 Jun 26 '09 at 22:04
  • 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 Jun 26 '09 at 22:19
  • 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 Jun 26 '09 at 22:20
  • 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 Jun 26 '09 at 23:52
  • Tim's second comment was the answer I was looking for. Thanks :) – Mikle Jun 27 '09 at 13:06