我有一个通用类:
class PaginatedTableViewController
<GenericElement, Source: PaginationDataSource
where Source.PaginatedGenericElement == GenericElement>:
UITableViewController
以及我尝试从 Storyboard实例化的另一个:
class CandidatesTableViewController:
PaginatedTableViewController<Match, MatchPaginationDataSource>
我在 Storyboard自定义类下拉菜单中找不到 CandidatesTableViewController
。如果我强制它然后在代码中转换我的 Controller ,应用程序会在运行时崩溃,提示我的 Controller (应该是 CandidatesTableViewController 实例)实际上是 UITableViewController 实例。
Unknown class _TtC21MyProjectName29CandidatesTableViewController in Interface Builder file. Could not cast value of type 'UITableViewController' (0x1040917f8) to 'MyProjectName.CandidatesTableViewController' (0x1013a9890).
在我的项目中,这个 Controller 嵌入到另一个 Controller 中,这就是我转换它的原因:
tableViewController = (segue.destinationViewController as! CandidatesTableViewController)
有人知道如何解决这个问题吗?
最佳答案
不幸的是,通用 Swift 类对于 Objective-C 代码不可见,并且在 Interface Builder 中也不被支持(在 Storyboard和 xib 中)。我发现这两点密切相关。
作为解决方案,我建议您使用聚合:不要使 View Controller 通用,而是将一些逻辑提取到另一个(通用)类并在 View Controller 中使用它。
关于ios - Xcode 7 Swift 2 无法实例化通用 UITableViewController 的 UIViewController 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43403058/