ios应用程序中的setPreferredContentSize错误

dha*_*val 5 iphone tablet ipad ios ios-universal-app

在启动iPad模拟器时,在xcode 5中创建"IOS Project"会导致以下情况.该应用程序适用于iPhone配置.我已将目标设置为5及更高版本并删除了autolayout,因为它与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)
Run Code Online (Sandbox Code Playgroud)

r61*_*618 17

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

if ( [self respondsToSelector:@selector(setPreferredContentSize:)] ) ...
Run Code Online (Sandbox Code Playgroud)


Tee*_*rin 3

在iOS7中,UIViewController有了一个新的属性preferredContentSize。为iOS7制作的项目有以下方法:

- (void)awakeFromNib
{
    self.preferredContentSize = CGSizeMake(320.0, 480.0);
    [super awakeFromNib];
}
Run Code Online (Sandbox Code Playgroud)

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

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