Question

Why don´t work when I change the orientation of the simulator in xCode? I see equals the label on the ViewController all time and the NSLog doesn´t appear.

ViewController.m:

- (void)viewDidLoad
{
    [self orientacionActual];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientacionActual)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIDeviceOrientation) orientacionActual
{
    UIDeviceOrientation orientacionActual = [UIDevice currentDevice].orientation;
    return orientacionActual;
}

- (void) cambiaOrientacion:(NSNotification *) notificación
{
    if(UIDeviceOrientationIsLandscape([self orientacionActual]))
    {
        self.LabelEstoy.text = @"Landscape";
        NSLog(@"Landscape");
    } else if (UIDeviceOrientationIsPortrait([self orientacionActual]))
    {
        self.LabelEstoy.text = @"Portrait";
        NSLog(@"Portrait");
    }
}
Was it helpful?

Solution

Call UIApplicationDidChangeStatusBarOrientationNotification instead of device orientation changes. Also, I'm not sure what is your goal, but try to use this snippet:

Inside viewDidLoad:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                      selector:@selector(cambiaOrientacion:)
                          name:UIApplicationDidChangeStatusBarOrientationNotification
                        object:nil];

and below:

- (void)cambiaOrientacion:(NSNotification *)notificacion
{
    UIInterfaceOrientation orientation = [[[notification userInfo] 
       objectForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        self.LabelEstoy.text = @"Landscape";
        NSLog(@"Landscape");
    } else
    {
        self.LabelEstoy.text = @"Portrait";
        NSLog(@"Portrait");
    }
}

and orientacionActual method redudant in your code, and doesn't makes any sense.

Don't forget to add [[NSNotificationCenter defaultCenter] removeObserver:self] inside dealloc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top