adit
adit

Reputation: 33644

UIViewController inside UIView not getting rotation delegation

I have a UIViewController's view inside my UIScrollView subclass. The problem is my UIViewController is not getting the willRotate delegate called when I rotate the device. This is probably because UIView does not have a rotation delegate implemented in it. What is the best way to solve this?

Basically the structure is I have a MainViewController in which it has a UIScrollView. Inside this UIScrollView I have a subviews, which is the view of a bunch of UIViewControllers. The issue now is, it's not getting rotation calls when I rotate. One way to deal with this is to delegate from the MainViewController to those respective UIViewController. Is there a better/elegant way to solve this?

I am adding it as a subview from my UIScrollView not my MainViewController and you can't do UIViewControllers containment from a UIView. Correct me if I am wrong

Upvotes: 1

Views: 1091

Answers (2)

Matt
Matt

Reputation: 1586

Watch the video that I posted in my comment: https://developer.apple.com/videos/wwdc/2011/?id=102

Here is a short summary of view controller containment, which is the design you are looking for, not combining all your logic into one view controller or using delegation for something that is built into the SDK.

A view controller has a view and provides logic for that view and most likely some of it's subviews.

You might have a complex view hierarchy and complex logic for specific views in that hierarchy that warrant the view to have it's own view controller controlling it and it's subviews.

Say you have a MainViewController and it's view is a UIScrollView (or subclass). Inside that scrollview, you might have an assortment of complex views that warrant their own controller, so you have a class, SubViewController that has the subview of the scrollview as it's view.

SubViewController needs to have rotation and appearance method callbacks working correctly in order to implement your logic for the the subview and handle layout changes.

    [mainViewControllerInstance addChildViewController:subViewControllerInstance];

But, wait. The subview is still not in the view hierarchy.

    [mainViewControllerInstance.view addSubView:subViewControllerInstance.view];

You have successfully created a valid view controller hierarchy of two view controllers and set up the associated view hierarchy of their views.

You will now have the appropriate callbacks functioning, as MainViewController will forward them to SubViewController.

EDIT:

See the documentation for an over view of view controller containment: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Upvotes: 0

Mat
Mat

Reputation: 7633

I think you're going against the MVC pattern. You can't have a controller inside a view; instead you should have a controller that mediates the communictaion between the view and the user's input. In your case you could set the scrollview as self.view of the MainViewController, and then add the viewcontrollers views as subviews.

Upvotes: 1

Related Questions