View rotation notifications: why didRotateFromInterfaceOrientation: doesn't get called?
Asked Answered
P

2

13

I'm trying to detect any device orientation change so that I can update the views.

I want to update the views whether the orientation is portrait or landscape, so I implemented this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

I know that if I want to update the views to show properly for the current orientation, I will need to implement some of the following methods:

– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
– didAnimateFirstHalfOfRotationToInterfaceOrientation:
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

Now, the problem is that I don't understand why none of these methods get fired when I rotate the simulator.

I tried as well this piece of code:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

But still nothing. So I'm wondering, does the simulator fire rotation notifications? If yes, what am I doing wrong?

Pazit answered 24/5, 2011 at 13:15 Comment(3)
where did you implement this method?Microlith
Yes simulator send notification for rotation.Mencius
@Ishu: thanks for the info; @Deepak: if you are referring to the method didRotateFromInterfaceOrientation, I implemented it in the .m file of my view controller; if you are talking about [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications], I did this in the viewDidLoad of the view controllerPazit
P
5

Quick summary, change this:

[window addSubview:viewController.view];

to this:

[window setRootViewController:viewController];

I'm sorry if I took somebody's time for a silly mistake of mine. I found out why the method – willRotateToInterfaceOrientation:duration: would never get called. Something brought my attention to the navigation controller:

Only the root view controller's willRotate method is being called. Most likely you have a strange hierarchy of view controllers.

I found these post in another forum and I had a look at the app delegate. My code was as follows:

CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window addSubview:viewController.view];
[viewController release];
[self.window makeKeyAndVisible];

The problem was that I was not setting any view controller for the window, but I was just adding a view. It was a mistake I did due to the hurry to test something. I had to fix the code like this:

CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window setRootViewController:viewController];
[viewController release];
[self.window makeKeyAndVisible];
Pazit answered 27/5, 2011 at 8:25 Comment(0)
D
53

You need to add notification observer something like

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

and add the method

- (void) didRotate:(NSNotification *)notification
{   
      UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

       if (orientation == UIDeviceOrientationLandscapeLeft)
      {
        NSLog(@"Landscape Left!");
      }
}
Doorstone answered 24/5, 2011 at 13:20 Comment(3)
Thanks, this is just what I needed - there does seem to be a general recommendation that [UIApplication sharedApplication].statusBarOrientation is more reliable [than UIDevice orientation] so people may want to use the former - I certainly am.Rescue
Note that in the code above, UIDeviceOrientationDidChangeNotification should be a constant, not the actual value. It just happens that the value of the constant is UIDeviceOrientationDidChangeNotification :-)Hydroelectric
w/r/t statusBarOrientation versus currentDevice orientation, some quick tests on my part suggest that checking statusBarOrientation in this use-case actually returns the previous orientation, while currentDevice returns the current/new orientation. Also, CurrentDevice will include UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown, while statusBar never does.Interdict
P
5

Quick summary, change this:

[window addSubview:viewController.view];

to this:

[window setRootViewController:viewController];

I'm sorry if I took somebody's time for a silly mistake of mine. I found out why the method – willRotateToInterfaceOrientation:duration: would never get called. Something brought my attention to the navigation controller:

Only the root view controller's willRotate method is being called. Most likely you have a strange hierarchy of view controllers.

I found these post in another forum and I had a look at the app delegate. My code was as follows:

CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window addSubview:viewController.view];
[viewController release];
[self.window makeKeyAndVisible];

The problem was that I was not setting any view controller for the window, but I was just adding a view. It was a mistake I did due to the hurry to test something. I had to fix the code like this:

CGRect bound = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)];
TestViewController *viewController = [[TestViewController alloc] init];
[window setRootViewController:viewController];
[viewController release];
[self.window makeKeyAndVisible];
Pazit answered 27/5, 2011 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.