ios - 如何在另一个 ViewController 中将带有 Xib 的 ViewController 显示为 subview

标签 ios objective-c iphone xcode uiviewcontroller

我有一个 Base Class我继承了我的Parent Class从此Base Class .

现在我有另一个 ViewControllerXIB .我只想展示这个XIB as我的父类中的 View 。

我的姓名ViewController与 XIB 为: SearchResultDisplayVC

我正在使用我的基类将我的 XIB 显示为 View 的这段代码:

 - (void)showSearchResultsView
 {

SearchResultDisplayVC  *searchView =(SearchResultDisplayVC *)[[[NSBundle mainBundle] loadNibNamed:@"SearchResultDisplayVC" owner:[SearchResultDisplayVC class] options:nil] firstObject];


[self.view addSubview:searchView.view];

}

在我的父类中,我将其称为“
[self showSearchResultsView];

虽然我在编译时没有收到任何错误消息,但是当我运行我的应用程序时它崩溃并显示以下消息:
*** Terminating app due to uncaught exception
'NSUnknownKeyException',
reason: '[<SearchResultDisplayVC 0x1104bb380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

最佳答案

您必须在父 View 中添加为 subview Controller 。

  SearchResultDisplayVC * searchView = [[SearchResultDisplayVC alloc] initWithNibName:@"SearchResultDisplayVC" bundle:nil];
    self.addChildViewController(searchView)

关于ios - 如何在另一个 ViewController 中将带有 Xib 的 ViewController 显示为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692182/

相关文章:

ios - 如何在 iOS 8 的子类 UIView 中调用 touchesBegan?

objective-c - 如何为 SSL 握手配置 CFStream(或 NSStream)?

ios - 允许键盘双击 UITableViewCell 中的 UITextField

iphone - 如何使继承的类能够在 Objective-C 中看到父类的隐藏方法

iphone - 当应用程序进入前台时查找当前 View 。 IOS

objective-c - NSMutableDictionary 崩溃并显示 "mutating message sent to immutable object"

ios - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 '[AnyHashable : Any]'

ios - NSURLConnection 后的字符串变化

objective-c - isRegisteredForRemoteNotifications 总是返回 no

ios - 如何创建这种 "slide out view controller"?