【问题标题】:setPreferredContentSize error in ios appios应用程序中的setPreferredContentSize错误
【发布时间】:2013-08-29 03:53:44
【问题描述】:

在启动 iPad 模拟器时,在 xcode 5 中创建“IOS 项目”会导致以下问题。该应用程序适用于 iPhone 配置。我已将目标设置为 5 及更高版本,并删除了自动布局,因为它与 ios/xcode 5 不兼容。

我在启动 iPad 应用程序时收到以下错误。

2013-08-29 08:53:57.688 IOS Project[350:c07] -[MasterViewController    setPreferredContentSize:]: unrecognized selector sent to instance 0x9e2cc20
2013-08-29 08:53:57.692 IOS Project[350:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MasterViewController setPreferredContentSize:]: unrecognized selector sent to instance 0x9e2cc20'
*** First throw call stack:
(0x1cd012 0x14c4e7e 0x2584bd 0x1bcbbc 0x1bc94e 0xbe7b 0x624d36 0x85054a 0x8506c3 0x40871e 0x4089a2 0x407876 0x418cb5 0x419beb 0x40b698 0x1f5fdf9 0x1f5fad0 0x142bf5 0x142962 0x173bb6 0x172f44 0x172e1b 0x40717a 0x408ffc 0x6d3d 0x6ca5)

【问题讨论】:

    标签: iphone ios ipad tablet ios-universal-app


    【解决方案1】:

    虽然在识别问题时接受的答案是正确的,但我不会检查特定的设备版本,而是使用类似的东西

    if ( [self respondsToSelector:@selector(setPreferredContentSize:)] ) ...
    

    【讨论】:

    • 你的回答对我帮助很大,你。
    【解决方案2】:

    在 iOS7 中,UIViewController 有一个新属性 preferredContentSize。一个为iOS7做的项目有如下方法:

    - (void)awakeFromNib
    {
        self.preferredContentSize = CGSizeMake(320.0, 480.0);
        [super awakeFromNib];
    }
    

    因此,无论属性是否实现,它都会向您自己的控制器发送setPreferredContentSize: 消息。要解决此问题,您可能希望避免设置不存在的属性:

    - (void)awakeFromNib
    {
        if ([[[UIDevice currentDevice] systemVersion] compare:@"7" options:NSNumericSearch] != NSOrderedAscending) {
            self.preferredContentSize = CGSizeMake(320.0, 480.0);
        }
        [super awakeFromNib];
    }
    

    【讨论】:

      【解决方案3】:

      如果您想在您的应用中保持向后兼容性,请始终检查新版本 iOS 中是否存在新引入的方法。如果旧版本中不存在该方法,则不得调用该方法。 有一个方法respondsToSelector,通过它你可以知道某个特定方法的存在。
      所以在你的情况下,如果你想检查preferredContentSize,你可以这样做:

      if ([self respondsToSelector:@selector(preferredContentSize)]) {
          self.preferredContentSize = CGSizeMake(320.0, 600.0);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多