views:

324

answers:

3

Hi,

I created a view in the AppDelegate, that I add to the window like this:

[window addSubview:myView];
I wanna be able to check for the device orientation everytime I come back to this view, so I can make some modifications to it. How can I do that in the appDelegate?

A: 

You could implement one of these methods in the delegate to see when the application rotates:

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;

Or just check the orientation of the UIApplication status bar as needed with:

[[UIApplication sharedApplication] statusBarOrientation];

The device orientation, which may or may not match the interface orientation, is:

[[UIDevice currentDevice] orientation];
drawnonward
A: 

In Apples examples you get notified via the delegate method:

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    NSLog(@"orientationChanged");
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

And then to display a portrait screen:

- (void)updateLandscapeView
{
PortraitView *portraitView = [[PortraitView alloc] init];
portraitView.delegate = self;
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
    [self presentModalViewController: portraitView animated:YES];
    isShowingLandscapeView = YES;
    }
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
    [self dismissModalViewControllerAnimated:YES];
    isShowingLandscapeView = NO;
    }
[portraitView release];
}

Of course you have to design the PortraitView as a delegate class for this to work as intended.

Not the only way but I find it works well and its in Apples examples. I wouldn't do it in the Appdelegate though but rather your uiview, I don't know ur design though.

Rudiger
please see my new answer I will explain new one problem I have with your solution
ludo
A: 

Here is what I tried first when the app is loading for the first time in the didFinishLauching


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

- (void)orientationChanged:(NSNotification *)notification
{
    [self performSelector:@selector(showScreen) withObject:nil afterDelay:0];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

-(void)showScreen {

 UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
 if (deviceOrientation == UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight) {

  CGRect screenRect = [[UIScreen mainScreen] bounds];

 }

}

The landscape is detected but the screenRect show width=768 and height=1024 (I'm in an Ipad device).

ludo
I think the problem is that you are just checking if its been rotated. You aren't doing anything with it. If you present a new view you should see that the bounds are what you expect. The way I suspect the OS would handle it is the same as if it didn't respond to the orientation changed unless you do something in your showSceen. If that makes sense....
Rudiger
No I don't really understand, you told me to present a new View but how can i do that if my screenRect still indicate the portrait mode?
ludo
Create a new UIView and present it like you would any other ViewController except if you do anything in the nib make it in landscape. What you are doing in the code is if the orientation is landscape, present a new view that is in landscape, else dismiss the landscape view
Rudiger