views:

4176

answers:

4

What object is responsible for dipatching the UIViewController rotation method calls, i.e:

– shouldAutorotateToInterfaceOrientation:
– willRotateToInterfaceOrientation:duration:
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:

I imagine it is UIApplication (but maybe the AppDelegate or UIWindow).

The next question, is how does the object know which UIViewController to talk to?

How does it know which UIViewController has its view as the subview of the window?

Is there a message you can send or a property you can set (of some object) that sets the "Active" UIViewController for the app?

A: 

This is all Magic You Don't Ever Worry About. Just add your controller's root view to the window--or get a tab bar or navigation controller to do it for you--and it will receive these messages.

(But if you poke around with the debugger, you might come to the same conclusion I have: there's some sort of internal table mapping each controller's view back to the controller, and the messages are dispatched based on that link.)

Brent Royal-Gordon
It is indeed magic, +1. The root view controller is rotated and resized and the frames of subviews are altered and resized based on autoresizing masks. Dont know why people have down voted this poor guy!
Raj
+5  A: 

It seems that UIApplication is dispatching a message to the active view controller.

But how does your View Controller instance get these messages?

The message is forwarded to the first view controller whose view has been added to the UIWindow instance.

This boils down to 3 basic scenarios:

  1. The ViewController whose view is added directly to the UIWindow instance (single view app)

  2. The navigation Controller in a Navigation based app, then the navigation controller forwards the message to the active views view controller.

  3. The tab bar Controller in a tab bar based app, then the tab bar controller forwards the message to the active views view controller (or the active navigation controller).

The problem you will have, is if you build an app with multiple views, but DO NOT use a Navigation controller or Tab Bar controller. If you swap views in and out of the UIWindow instance manually, you will not receive these messages reliably. This is similar to posts like this one: http://stackoverflow.com/questions/131062/iphone-viewwillappear-not-firing

Just use Apple's conventions for multiple views and you be fine. Hope this saves some one an hour or two

Corey Floyd
One doubt though, does navigation controller / tab bar controller pass the messages to the subView's controllers or rather they just resize the views? I think that they only resize their subviews, the first view controller does not pass the message down its view hierarchy, they only resize their subviews.
Raj
exactly, Apple recommends only using one VC per screen. So using "sub" view controllers is not supported and the messages are not automatically forwarded.
Corey Floyd
+1  A: 

How does it know which UIViewController has its view as the subview of the window?

UIViewController class maintains a static map between views and their view controllers. This map is queried in a few key places inside CocoaTouch. In particular, [UIView nextResponder] queries it and returns the controller if found.

I guess UIWindow does the same lookup with its root view to know which controller to forward rotation events to. Will check this next time I'll be looking something up in the disassembly. (I've spent some time reverse-engineering CocoaTouch to understand how things work there.)

Andrey Tarantsov
A: 

I have a similar problem.

I have a game working in landscape orientation (both left and right landscape).

In order to manage a multi-view application I use a root UIviewController, i.e. a dummy UIViewcontroller with a UIview that does nothing. I then add every other UIview to it as a subview.

When starting the app, the emulator quickly rotates to a portrait orientation, and i get every one of my views with it's frame property looking like (0,-80,320,480). This results in the view being clipped by a 160 X 320 rectangle on the right side.

I've noticed that the – shouldAutorotateToInterfaceOrientation: method of each subview is only called once, and that happens when it is added to the root view. Later on, when rotating the device, only the root view gets it's method called.

Unsurprisingly, any one of the – willAnimate* methods calls are only sent to the root view, so, defining any of these methods in subviews is pointless.

These methods are called everytime the orientation is changed to an orientation accepted by the – shouldAutorotateToInterfaceOrientation: method. so I figured i could chnage my subviews' frame property from there.

To accomplish this, I defined the following method in my rootViewController class:

  • (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{

    subview1_ViewController.view.frame = CGRectMake(0,0,480,320);

    subview2_ViewController.view.frame = CGRectMake(0,0,480,320);

    ...
    

}

I don't understand why the frame property is not updated, but I know this workaround works.

Hope this helps....

OliveiraX