首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用NSNotificationCenter和UIDeviceOrientation改变方向

用NSNotificationCenter和UIDeviceOrientation改变方向
EN

Stack Overflow用户
提问于 2014-01-16 18:03:44
回答 1查看 4.1K关注 0票数 1

当我在xCode中改变模拟器的方向时,为什么不工作?我看到ViewController上的标签一直都是相等的,而NSLog也没有出现。

ViewController.m:

代码语言:javascript
复制
- (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");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-16 18:21:37

调用UIApplicationDidChangeStatusBarOrientationNotification,而不是更改设备方向。另外,我也不知道你的目标是什么,但是试着使用下面的代码片段:

内部viewDidLoad:

代码语言:javascript
复制
    [[NSNotificationCenter defaultCenter] addObserver:self 
                      selector:@selector(cambiaOrientacion:)
                          name:UIApplicationDidChangeStatusBarOrientationNotification
                        object:nil];

以下是:

代码语言:javascript
复制
- (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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21169450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档