[英]Change orientation with NSNotificationCenter and UIDeviceOrientation
Why don´t work when I change the orientation of the simulator in xCode? 当我在xCode中更改模拟器的方向时,为什么不起作用? I see equals the label on the ViewController all time and the NSLog doesn´t appear. 我看到所有时间都等于ViewController上的标签,并且没有出现NSLog。
ViewController.m: 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");
}
}
Call UIApplicationDidChangeStatusBarOrientationNotification
instead of device orientation changes. 调用UIApplicationDidChangeStatusBarOrientationNotification
而不是设备方向更改。 Also, I'm not sure what is your goal, but try to use this snippet: 另外,我不确定您的目标是什么,但请尝试使用以下代码段:
Inside viewDidLoad:
内部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. 和orientacionActual
方法在您的代码中orientacionActual
,没有任何意义。
Don't forget to add [[NSNotificationCenter defaultCenter] removeObserver:self]
inside dealloc
. 不要忘记在dealloc
内添加[[NSNotificationCenter defaultCenter] removeObserver:self]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.