1

I have is a simple if statement to detect an orientation and perform an action. This works well, but it only works the first time and it can't detect it again.

Does this void only get called once and if so, how can I change this around to constantly check?

Would I need to move some to viewDidLoad?

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)

        {
            [self.navigationController pushViewController:graphView animated:YES];

        }

            else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            {
                [self.navigationController pushViewController:graphView animated:YES];
            }

            else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
            {
                [self.navigationController popToRootViewControllerAnimated:YES];
                NSLog(@"portrait");
            }

            else
            {
                [self.navigationController popToRootViewControllerAnimated:YES];

            }

        }
7
  • What are you trying to accomplish? Jul 28, 2013 at 21:23
  • Im trying to switch back and forth between view controllers using a UINavigationController depending on the orientation Jul 28, 2013 at 21:25
  • Is this code in your navigation controller or in the view controllers embedded in the nav controller?
    – user
    Jul 28, 2013 at 21:26
  • It is in the first main view controller with the navigation controller embedded Jul 28, 2013 at 21:26
  • is it because I load the new view controller and no longer can detect the orientation since that was in the last view controller? Jul 28, 2013 at 21:30

2 Answers 2

1
[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                             name:@"UIDeviceOrientationDidChangeNotification" 
                                           object:nil];
1
  • do I put that in view did load and then in the orientationChanged method create the if statements to detect the exact orientation? Jul 28, 2013 at 21:28
1

Expounding on what @Vjy wrote- One solution is to listen for device orientation notifications, and then find the new orientation and respond to it.

Before getting any device orientation notifications though, you must call

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

or no notifications will be sent in the first place. After you begin generating orientation notifications, you have to listen for them. You will need to tell each relevant view controller to listen for these notifications with

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(orientationDetected) //this is your function
                                             name:@"UIDeviceOrientationDidChangeNotification" 
                                           object:nil];

then in orientationDetected or whatever you wish to name it-

- (void) orientationDetected
{
    switch ([[UIDevice currentDevice] orientation])
    {
        case UIDeviceOrientationLandscapeLeft:
             // push appropriate view controller
             break;

        case UIDeviceOrientationPortrait:
             // and so on...
             break;
    }
}

You could also change your notification selector method to @selector(methodThatReceivesNote:) (note the colon) and have your method take a (NSNotification*) param, and then look through the [paramName userInfo] to find the relevant orientation, although I found very little info on what UIDevice notifications actually include in that userInfo.

You could also just research how to handle device orientation changes in a navigation controller's sub-views. I looked around a tiny bit, but I couldn't really find much about this. I'm sure the information is somewhere though, and this might be a more robust solution than notifications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.