当我在xCode中改变模拟器的方向时,为什么不工作?我看到ViewController上的标签一直都是相等的,而NSLog也没有出现。
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");
}
}发布于 2014-01-16 18:21:37
调用UIApplicationDidChangeStatusBarOrientationNotification,而不是更改设备方向。另外,我也不知道你的目标是什么,但是试着使用下面的代码片段:
内部viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cambiaOrientacion:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];以下是:
- (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");
}
}和orientacionActual方法在您的代码中修改,没有任何意义。
不要忘记在[[NSNotificationCenter defaultCenter] removeObserver:self]中添加dealloc。
https://stackoverflow.com/questions/21169450
复制相似问题