首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册UIDeviceOrientation

注册UIDeviceOrientation
EN

Stack Overflow用户
提问于 2012-12-19 00:17:18
回答 3查看 444关注 0票数 1

我想要检测设备的当前方向。

因此,我使用了一行代码

注意:即使我没有注册它,它也会给我提供方向

代码语言:javascript
复制
-(IBAction) deviceOrientationChanged:(id) sender{
    NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}

当我更改方向时,在一个按钮上单击事件,我会得到设备方向。

但是,document说你需要先注册它,然后再使用上面的代码。

注意:在下面的代码中,我注册了接收设备方向通知的设备

代码语言:javascript
复制
-(IBAction) deviceOrientationChanged:(id) sender{

    if(![[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]){
        //This statement is called very time when I click on the button. I expected to execute only once.
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}

因此,我不能通过完美的方式来获得设备方向。

EN

回答 3

Stack Overflow用户

发布于 2012-12-19 07:48:52

我认为您的if条件是错误的;您拥有它,就好像([UIDevice currentDevice isGeneratingDeviceOrientationNotifications])它应该是

if( ! [UIDevice currentDevice isGeneratingDeviceOrientationNotifications])

票数 1
EN

Stack Overflow用户

发布于 2012-12-21 01:27:54

注册通知与生成通知不同。通常,如果您计划在应用程序代理中或在适当的位置使用设备方向通知,您将在UIDevice上调用beginGeneratingDeviceOrientationNotifications,并在您不再需要通知时将endGeneratingDeviceOrientationNotifications放在另一个适当的位置。这只是deviceOrientaion通知的初始设置。

要获得这些通知,您的控制器或对象必须通知我们通知中心。U注册通知UIDeviceOrientationDidChangeNotification。[对象添加观察者:myObject selector:@selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification NSNotificationCenter:nil]

当你不需要它时,或者在取消分配对象时,将其从通知中心删除。

票数 1
EN

Stack Overflow用户

发布于 2012-12-19 00:20:48

您无法停止设备生成UIViewController循环通知。请参阅问题here.

您链接到的文档是指NSNotificationCenter通知。

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

https://stackoverflow.com/questions/13937114

复制
相关文章

相似问题

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