didRotateFromInterfaceOrientation not firing when rotating?

13,289

Solution 1

Well, I never did figure out why the events were not firing, but I did figure out a workaround:

In the two UISplitViewController delegate methods, splitViewController:willHideViewController:withBarButtonItem:forPopoverController: and splitViewController:willShowViewController:invalidatingBarButtonItem:, I'm detecting whether or not my view is visible, and then doing my rotation logic here.

Solution 2

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.

Share:
13,289
DOOManiac
Author by

DOOManiac

Just some guy who does PHP, JS, & MySQL web development.

Updated on June 12, 2022

Comments

  • DOOManiac
    DOOManiac almost 2 years

    Possible Duplicate:
    ViewController not responding to didRotateFromInterfaceOrientation

    I'm having trouble with the didRotateFromInterfaceOrientation method not firing in one of my viewcontroller subclasses.

    I have an iPad app w/ UISplitViewController as the main view. On the Detail side, I'm using a "hidden" (no toolbar,navbar) navigation controller for lazy view switching. The ViewController I'm wanting to catch didRotateFromInterfaceOrientation on is two levels deep in the navcontroller hierarchy. (None of this should make a difference, but I'm including this info in case there's some particular case that I don't know about)

    I have:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }
    
    // This doesn't work. :(
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        NSLog(@"Rotate Go!");
    }
    

    The view rotates just fine, but didRotateFromInterfaceOrientation never fires.

    Any idea what I'm missing?

    • Felipe Sabino
      Felipe Sabino about 13 years
      Do you have a uitabbar? Because if you are, you should create a uitabbar class and implement the "shouldAutorotateToInterfaceOrientation" at this class as well, so that all other controllers receive the rotation message.
    • DOOManiac
      DOOManiac about 13 years
      Nope. I'm using a UISplitViewController, with a UIToolbar and UINavigationController (with the toolbar hidden, so different views share the other toolbar). But no UITabBar...
    • Felipe Sabino
      Felipe Sabino about 13 years
      According to apple, your UISplitViewController must be the root view in your application window. You can experience some weirdness if thatbis not the case. Check this SO thread stackoverflow.com/questions/2734016/…
    • DOOManiac
      DOOManiac about 13 years
      My UISplitViewController is the root view of the window. I'm making sure to return YES to shouldAutorotateToInterfaceOrientation from all of my subviews. The actual rotation works fine, its just that didRotateFromInterfaceOrientation is never called.
    • Felipe Sabino
      Felipe Sabino about 13 years
      So, just a couple more question just to make it more clear. Are your controller extending any class other than UIViewController? Are you pushing your UIViewController or just adding the controller view as a subview of another controller? your are returning YES at shouldAutorotateToInterfaceOrientation, but is this method really getting called when you rotate or just the didRotateFromInterfaceOrientation?
    • DOOManiac
      DOOManiac about 13 years
      I have a DetailViewController (subclass of UIViewController) as the Detail side of the UISplitViewController. Rather than swapping out detail views, which gave me problems, I'm taking the lazy way out by having a UINavigationController on DetailViewController, with both the toolbar and navigation bar hidden. I'm pushing my other classes (which are all UIViewController subclasses with their own XIBs) onto the UINavigation stack to handle all changing of the detail views.
    • DOOManiac
      DOOManiac about 13 years
      (continued) I am definitely returning YES to shouldAutorotateToInterfaceOrientation in all subclasses, as they rotate just fine. But didRotateFromInterfaceOrientation never fires. :(
  • cberkley
    cberkley about 12 years
    Thanks! This just fixed a problem that I had with UISplitViewController not getting rotational events.