我想要检测设备的当前方向。
因此,我使用了一行代码
注意:即使我没有注册它,它也会给我提供方向
-(IBAction) deviceOrientationChanged:(id) sender{
NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}当我更改方向时,在一个按钮上单击事件,我会得到设备方向。
但是,document说你需要先注册它,然后再使用上面的代码。
注意:在下面的代码中,我注册了接收设备方向通知的设备
-(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]);
}因此,我不能通过完美的方式来获得设备方向。
发布于 2012-12-19 07:48:52
我认为您的if条件是错误的;您拥有它,就好像([UIDevice currentDevice isGeneratingDeviceOrientationNotifications])它应该是
if( ! [UIDevice currentDevice isGeneratingDeviceOrientationNotifications])
发布于 2012-12-21 01:27:54
注册通知与生成通知不同。通常,如果您计划在应用程序代理中或在适当的位置使用设备方向通知,您将在UIDevice上调用beginGeneratingDeviceOrientationNotifications,并在您不再需要通知时将endGeneratingDeviceOrientationNotifications放在另一个适当的位置。这只是deviceOrientaion通知的初始设置。
要获得这些通知,您的控制器或对象必须通知我们通知中心。U注册通知UIDeviceOrientationDidChangeNotification。[对象添加观察者:myObject selector:@selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification NSNotificationCenter:nil]
当你不需要它时,或者在取消分配对象时,将其从通知中心删除。
发布于 2012-12-19 00:20:48
您无法停止设备生成UIViewController循环通知。请参阅问题here.
您链接到的文档是指NSNotificationCenter通知。
https://stackoverflow.com/questions/13937114
复制相似问题