2

I want to create a two player game on iPad, but I don't really know how should I do it right. My concept will be just like "fruit ninja" of two player game (one vs other).

I mean that one player will play in viewcontrollerA and the other one will play in viewcontrollerB (that should be upside down from the viewcontrollerA).

How should I do it? Do I need to create two view controllers and display then via controller containment, or there is another and better way to do that? thanks!

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user1492776
  • 280
  • 2
  • 7
  • 14

2 Answers2

6

In iOS5 there is a blessed way for controllers to receive orientation events, add/remove callbacks, and they are properly inserted into the responder chain. So if you are going to do it, this is the best way. As long as you don't do silly things like adding the same view to two UIViewControllers you are fine.

Why do it? I see logical to put the game engine on the master controller, write a client, encapsulate that client on a child view controller, and reuse it for the two players.

You could also use nested controllers to encapsulate complexity if you feel you need such thing. Example: one for the interface talking to the game engine, and another for the screen getting events from the engine.

UIViewController containment is easy. You need to read 6 methods and understand what calls what. You are welcome to read the documentation, but in a nutshell:

For example, to nest one controller in another, add the child's view as a regular view, and call addChildViewController: and didMoveToParentViewController: in the order shown below:

// from the parent view controller
ChildViewController *child = [ChildViewController new];
[self addChildViewController:child];
[self.view addSubview:child.view];
[child didMoveToParentViewController:self];
Jano
  • 62,815
  • 21
  • 164
  • 192
1

Best way would be to just have a root view controller that would be the entire screen, and then two sub-views that is each players screen. You don't want to nest View Controllers

JoeCortopassi
  • 5,083
  • 7
  • 37
  • 65
  • 5
    iOS 5.0 added view controller containment APIs because people *do* want to nest view controllers. – Filip Radelic Jul 10 '12 at 17:42
  • You can nest view controllers, but I think we can both agree that that is not what is needed here, right? – JoeCortopassi Jul 10 '12 at 17:44
  • 1
    Well, it's not needed, but it might be better practice looking forward. I see no reason to discourage someone from using it, that's all. – Filip Radelic Jul 10 '12 at 17:48
  • Arguably, it *IS* what is needed here, since each view is being controlled by a different player, therefore, a different controller for each view, and a master controller to contain both – Dan F Jul 10 '12 at 17:51
  • http://stackoverflow.com/questions/1486832/how-to-add-an-uiviewcontrollers-view-as-subview/1765714#1765714 – JoeCortopassi Jul 10 '12 at 18:00
  • 3
    I see your link from 2009 and raise you [WWDC 2011 session video "Implementing UIViewController Containment"](https://developer.apple.com/videos/wwdc/2011/?id=102). – Jano Jul 10 '12 at 22:04
  • I never said you *can't* nest view controllers, I just said that is not what you want to do here. Unless the OP left something big out, I don't see why having rotation callbacks (or any of the other stuff that differentiate views and view controllers) adds any value – JoeCortopassi Jul 10 '12 at 22:13