开发者

View Controller inside View Controller won't receive rotation messages - ios

I have a customized View Controller (we'll call it the wrapper). Its view contains only a UIScrollView. The scroll view contains another customized view controller (we'll call it the inside view), initialized from an xib file (the scroll view itself is initialized from a xib file as well, but I don't believe it matters).

The wrapper view is displayed using a UITabBarController, which contains several more similar view controllers.

I have this weird problem: the wrapper's rotation functions - shouldAutoRotate, willAnimateRotation - get called every time that I rotate the device. For some reason, the inside view's rotation functions don't get called, but it still rotates. The inside view's shouldAutoRotate does get called when initializing it (when the app starts).

I've looked at google and couldn't find anything that is relevant to my case. I'm not sure if it is relat开发者_Go百科ed, but the Autoresize subviews is checked on all xib files.

I'd be glad if you could help me solve this problem. I need the inside view's rotation function to get called on rotation in order to arrange it manually, but I'd like to avoid calling them from the wrapping view (rather it to work as it should).

Thank you in advance!


Well the innerViewController's rotation function will not be called because they are added as subview's to your scrollView what you can do is generate a NSNotification when orientation changes in your parent controller then you can receieve notification in subview and manage them accordingly. Or you can iterate through subviews of UIScrollView when your shouldAutoRoatate called in you parent controller and then manually call should autorotate method of child views. Hope you understand.


The simplest way is to hold some UIInnerViewController* innerController in your .h file and in .m to call inner's - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation inside of the wrapper like so:

wrapper.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    BOOL innerResult = [innerController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    //may be more computations here
    return innerResult;// or any other value, based on your needs
}

Other approach that you may use is to register inner controller to UIDeviceOrientationDidChangeNotification like so:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(orientationChanged:) 
                                      name:UIDeviceOrientationDidChangeNotification 
                                      object:nil];

and layout subviews in -(void)orientationChanged:(NSNotification*)notification;in the same inner controller. The only thing you should be awere of, is that UIDeviceOrientation is a little-bit different than UIInterfaceOrientation and may hold value such UIDeviceOrientationFaceUp that is not applicable to UI changes in most cases.


Nesting viewcontrollers inside custom viewcontrollers is not supported in iOS 4. You can usually forward all the necessary messages to your child VC's manually, though, with acceptable results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜