开发者

ViewController not responding to didRotateFromInterfaceOrientation

开发者 https://www.devze.com 2023-01-19 04:37 出处:网络
My view controller is not responding to didRotateFromInterfaceOrientation, despite that I have added following in my code:

My view controller is not responding to didRotateFromInterfaceOrientation, despite that I have added following in my code:

- (BOOL)shouldAutorotateToInt开发者_如何学GoerfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    [self.popOver dismissPopoverAnimated:NO];
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    ... My Custom Code...   
    }
}

Am I doing something wrong here?


If you can't inherit from UIViewController (which is unfortunate), you can use this:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Then register to start receiving UIDeviceOrientationDidChangeNotification notifications.


If your UIViewController is a child in some root view then IB does not add it as a child controller to the root controller by default. The easiest way to address this is to modify your root controller:

- (void)viewDidLoad
{
    [super viewDidLoad];    
    [self addChildViewController:(UIViewController*) self.yourChildController];
}  

This should do the trick. Now your child controller will be receiving both:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

and

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

messages.


I think the real answer here (more accurately the answer to the linked question) is that you need to call

[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

in your subclass implementation of the didRotateFromInterfaceOrientation method. For example:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    // Then your code...
}

This is not mentioned in the apple documentation but caused some serious and unexplained problems for me when omitted...

0

精彩评论

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