SENTHIL KUMAR
SENTHIL KUMAR

Reputation: 647

Device Orientation Changed notification event not called in other controllers other than main view controller

In my application i have 3 view controllers each is navigating by a button click. In my first view controller i have added the command

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Orientchanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

it works only on my first UIViewController which is added in appdelegate. But it donot work for other UIViewControllers and call the selector event. What is the reason? Should I add other view controllers to appdelagate.


An example of the code in the Orientchanged: method:

- (void)Orientchanged:(NSNotification *)notification 
{
    UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
    scrollWidth_L = 1024;
    scrollWidth_P = 768;
}

Upvotes: 0

Views: 3674

Answers (2)

Sverrisson
Sverrisson

Reputation: 18157

Add these methods:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    UIInterfaceOrientation devOrientation = self.interfaceOrientation;
    CGFloat scrollWidth_L = self.view.bounds.size.width;
    CGFloat scrollWidth_P = self.view.bounds.size.height;

    if (UIInterfaceOrientationLandscapeLeft == devOrientation || UIInterfaceOrientationLandscapeRight == devOrientation) {
        // Code for landscape setup

    } else {
        // Code for portrait setup

    }
}

Upvotes: 3

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

You have added an observer for the specific viewController. You will have to add in all other viewControllers for knowing the current visible viewController orientation changed.

If you want to know globally then add observer in appDelegate.

Note : Don't forget to remove observer when not needed.

EDIT : Observer depends on where it contains. Here in your case addObserver:self , self is your first view controller.

Upvotes: 1

Related Questions