views:

256

answers:

4

Hello guys!

Down there is my code which is called when the iPhone is rotated.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
switch (interfaceOrientation) {
    case UIInterfaceOrientationPortrait:
                    // some action
        return YES;
    case UIInterfaceOrientationLandscapeLeft: {
                    // some action
        return NO;
    }
    case UIInterfaceOrientationLandscapeRight: {
                    // some action
        return NO;
    }
    default:
        return NO;
        break;
}

}

I thought this is enough, but then I spotted, that the

case UIInterfaceOrientationPortrait:

is never called. What can I do? Do I have to use the default? Maybe it isn't the best solution, or it is?

A: 

That should be adequate if you just want to use the portrait orientation.

Where have you overriden this function? It should be in the topmost view controller.

Mongus Pong
this is in my topmost view controller... it is called everytime I change to landscape, but when I change back to portrait it isn't called... I don't know why :(
Infinity
How can it change to landscape, you are returning NO, so it cant. That is probably why it doesnt call when going back to portrait, because you are ensuring it is always portrait. Try returning YES when changing to Landscape and see if the portrait call is made then.
Mongus Pong
I don't want to change to landscape, I'm just doing some actions when the phone changes to landscape, but I don't want to be changed the entire view to landscape, just some icons.
Infinity
A: 

Hey guys, come on, suggest me a good way to this. How can I do some action when rotate the iPhone to landscape if I don't want to rotate the whole view?

Infinity
A: 

I've just given your code to a sample application, and its working fine, with the given code, when i change the orientation to landscape, nothing is happening to the orientationo f anything in the view. One method is to design view separately for each orientation, and return YES to all of the orientations and use the following property

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) 
    interfaceOrientation duration:(NSTimeInterval)duration { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait 
        || interfaceOrientation == 
            UIInterfaceOrientationPortraitUpsideDown) { 
       //design your portrait view eg: for button, button.frame = CGRectMake(x,y,w,h)
    } 
    else { 
        //design for the landscape
    } 
} 
Nithin
+2  A: 

Ok. I found the answer. There is a notification when you rotate the phone.

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

and

- (void)receivedRotate:(NSNotification*)notif {
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
switch (interfaceOrientation) {
    case UIInterfaceOrientationPortrait: {
        break;
    }
    case UIInterfaceOrientationLandscapeLeft: {
        break;
    }
    case UIInterfaceOrientationLandscapeRight: {
        break;
    }
    default:
        break;
}   

}

Use it ;)

Infinity