gpt4 book ai didi

ios - 使用NSNotificationCenter和UIDeviceOrientation更改方向

转载 作者:行者123 更新时间:2023-12-01 17:40:35 28 4
gpt4 key购买 nike

当我在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");
}
}

最佳答案

调用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

关于ios - 使用NSNotificationCenter和UIDeviceOrientation更改方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169450/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com