2010-07-02 34 views
4

我构建了一款面向iOS 3.1.3和更高版本的应用,并且我遇到了UIKeyboardBoundsUserInfoKey的问题。原来它在iOS 3.2及更高版本中已弃用。我所做的是使用下面的代码使用取决于iOS版本右键:iOS中的弃用常量

if ([[[UIDevice currentDevice] systemVersion] compare:@"3.2" options:NSNumericSearch] != NSOrderedAscending) 
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds]; 
else [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; 

这实际上工作正常,但Xcode的警告我说,UIKeyboardBoundsUserInfoKey已被弃用。我怎样才能摆脱这个警告,而不必抑制任何其他警告?

另外,有没有办法简单地检查UIKeyboardBoundsUserInfoKey是否定义为避免检查iOS版本?我试着检查它是否是NULLnil,甚至是弱连接UIKit,但似乎没有任何工作。

在此先感谢

回答

4

由于的存在弃用固定在你的代码将引发警告(和破坏构建我们-Werror用户)的任何地方,你可以使用实际的恒定值查找字典。谢谢Apple通常(总是?)使用常量名称作为它的值。

至于运行时检查,我想你最好testing for the new constant

&UIKeyboardFrameEndUserInfoKey!=nil 

所以,这就是我实际上做让键盘框(在此基础上other answer):

-(void)didShowKeyboard:(NSNotification *)notification { 
    CGRect keyboardFrame = CGRectZero; 

    if (&UIKeyboardFrameEndUserInfoKey!=nil) { 
     // Constant exists, we're >=3.2 
     [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame]; 
     if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
      _keyboardHeight = keyboardFrame.size.height; 
     } 
     else { 
      _keyboardHeight = keyboardFrame.size.width; 
     } 
    } else { 
     // Constant has no value. We're <3.2 
     [[notification.userInfo valueForKey:@"UIKeyboardBoundsUserInfoKey"] getValue: &keyboardFrame]; 
     _keyboardHeight = keyboardFrame.size.height; 
    } 
} 

我实际上在3.0 Device和4.0 Simulator上测试了这个。

+0

谢谢!这正是我所期待的。 – Pablo 2010-07-02 16:48:03

+1

如果有其他人遇到此代码,请注意,它并不完全符合您的要求。返回的keyboardFrame不考虑接口方向,并且if(UIInterfaceOrientationIsPortrait)行会在各种情况下给出错误答案(特别是当设备平放在桌子上而不是直立时)。相反,你想要做的就是使用[self.view convertRect:keyboardFrame fromView:nil]将未经变换的窗口坐标转换为视图坐标。这将旋转框架,使高度始终是正确的使用。 – MrCranky 2011-01-31 13:45:11