【问题标题】:Using same programmatically loaded Storyboard for iOS and iPad为 iOS 和 iPad 使用相同的以编程方式加载的 Storyboard
【发布时间】:2014-06-24 14:31:29
【问题描述】:

我有一个 Storyboard,主要用于配置一些模态视图控制器及其随附的 UINavigationControllers 和 UITableViewCells。

我的应用是通用应用,所以我想同时支持 iPhone 和 iPad。但是,我用于模态视图控制器的附加故事板并不特定于任何设备(感谢 AutoLayout),因此我可以在两者中都使用它们。

目前我像这样以编程方式加载情节提要的初始视图控制器:

UIStoryboard *companyStoryboard = [UIStoryboard storyboardWithName:@"CompanySelection" bundle:nil];

当 Xcode 询问我想为什么设备创建 Storyboard 时,我选择了“iPad”创建了 CompanySelection.xib。所以它在 iPad 上运行时运行良好。但是,它会在 iPhone 上崩溃:

2014-06-24 16:13:01.720 Notes[24630:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'CompanySelection' in bundle NSBundle </path/to/library/Application Support/iPhone Simulator/7.1-64/Applications/E78E3994-0147-4BD5-AAB6-EA452707EC4D/MyApp.app> (loaded)'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102dd7495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000102b3699e objc_exception_throw + 43
    2   UIKit                               0x0000000101d6c2b7 +[UIStoryboard storyboardWithName:bundle:] + 542
    3   Notes                               0x000000010007f652 -[GRMasterViewController insertNewObject:] + 210 // the call which loads a view controller from a storyboard and should display it modally
    4   UIKit                               0x00000001018b3f06 -[UIApplication sendAction:to:from:forEvent:] + 80
    5   UIKit                               0x00000001018b3f06 -[UIApplication sendAction:to:from:forEvent:] + 80
    6   UIKit                               0x00000001018b3eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    7   UIKit                               0x0000000101990880 -[UIControl _sendActionsForEvents:withEvent:] + 203
    8   UIKit                               0x000000010198fdc0 -[UIControl touchesEnded:withEvent:] + 530
    9   UIKit                               0x00000001018ead05 -[UIWindow _sendTouchesForEvent:] + 701
    10  UIKit                               0x00000001018eb6e4 -[UIWindow sendEvent:] + 925
    11  UIKit                               0x00000001018c329a -[UIApplication sendEvent:] + 211
    12  UIKit                               0x00000001018b0aed _UIApplicationHandleEventQueue + 9579
    13  CoreFoundation                      0x0000000102d66d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x0000000102d665f2 __CFRunLoopDoSources0 + 242
    15  CoreFoundation                      0x0000000102d8246f __CFRunLoopRun + 767
    16  CoreFoundation                      0x0000000102d81d83 CFRunLoopRunSpecific + 467
    17  GraphicsServices                    0x0000000103ca2f04 GSEventRunModal + 161
    18  UIKit                               0x00000001018b2e33 UIApplicationMain + 1010
    19  MyApp                               0x0000000100001c53 main + 115
    20  libdyld.dylib                       0x000000010335c5fd start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

根据this question,在选择Main-Storyboard时,可以重复使用iPhone Storyboard,所以一般来说应该是可以的。但是,我确实希望拥有不同的主要故事板,并且只是结合一些其他的故事板,它们没有任何区别。 当然,我可以复制我现有的 iPad 故事板并根据userInterfaceIdiom 加载正确的故事板,但是我每次都必须更改两个故事板。需要改变,这似乎很容易出错,而且需要做很多不必要的工作。

我也尝试了another idea,建议手动编辑.xib-XML 将targetRuntime="iOS.CocoaTouch.iPad" 更改为targetRuntime="iOS.CocoaTouch"(我认为不需要其他更改,因为它们仅与IB 中的显示有关)但是.xib 仍未找到。

有没有办法让 Storyboard 通用或以某种方式强制操作系统为两个设备加载相同的 Storyboard?

PS:我知道 Xcode 6 有通用的 Storyboard,它们应该可以在 iOS 7 上运行。但是,在使用 Xcode 6 编译时,我的应用程序中的其他一些东西不起作用,所以我更喜欢使用 Xcode 5现在。

【问题讨论】:

    标签: ios objective-c uistoryboard xcode-storyboard ios-universal-app


    【解决方案1】:

    好吧,和往常一样,错误是不同的。前段时间,我将 Storyboard 文件移到了一个库项目中。但是,iPad 应用程序继续工作,因为旧的 Storyboard 仍然包含在 DerivedData 文件夹中。删除 DerivedData 文件夹后,它现在在 iPad 上也失败了,这让我发现我指定了错误的包。

    因此得出结论:如果您按名称加载,则在 iPhone 和 iPad 上使用相同的 Storyboard 文件是没有问题的。 “targetRuntime”的设置无关紧要(我有一个带有 targetRuntime="iOS.CocoaTouch.iPad" 的 Storyboard 文件和一个带有 targetRuntime="iOS.CocoaTouch" 的 Storyboard 文件,两者都加载到两种设备类型上,我看不出 IB 有什么区别)。

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 1970-01-01
      • 2013-08-09
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2012-01-24
      相关资源
      最近更新 更多