简体   繁体   English

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

[英]How to show a ViewController with Xib in Another ViewController as a SubView

I have one Base Class I have inherited my Parent Class from this Base Class . 我有一个Base Class我从这个Base Class继承了我的Parent Class Base Class

Now I have another ViewController With XIB . 现在我有另一个带XIB ViewController All I want is to present This XIB as a View in my Parent Class. 我想要的只是在我的父类中将此XIB as呈现XIB as视图。

Name of My ViewController With XIB is: SearchResultDisplayVC 我的ViewController名称使用XIB是: SearchResultDisplayVC

This code I am using my Base Class to Show My XIB as a View: 这段代码我使用我的基类来显示我的XIB作为视图:

 - (void)showSearchResultsView
 {

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


[self.view addSubview:searchView.view];

}

In my Parent class I am calling it as" 在我的Parent类中,我将其称为“

[self showSearchResultsView];

Although I am not getting any error message in compile time but when I run my App it crashes showing the following message: 虽然我没有在编译时收到任何错误消息但是当我运行我的应用程序时它崩溃显示以下消息:

*** Terminating app due to uncaught exception
'NSUnknownKeyException',
reason: '[<SearchResultDisplayVC 0x1104bb380> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'

you have to add as child view controller in the parent view. 您必须在父视图中添加为子视图控制器。

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

Please modify your "showSearchResultsView" as: 请将您的“showSearchResultsView”修改为:

- (void)showSearchResultsView
 {

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


[self.view addSubview:searchView];

}

You have got a problem with your SearchResultDisplayVC.xib file I hope, Check the outlet connection for view in the file's owner it should be set to the main view for any view controller to work. 你的SearchResultDisplayVC.xib文件有问题我希望,检查文件所有者视图的插座连接,它应该设置为主视图,以便任何视图控制器工作。 Like below, 如下,

在此输入图像描述

All view controllers expect a "view" object to be defined and mapped from nib files. 所有视图控制器都希望从nib文件定义和映射“视图”对象。 You have a view controller nib but there is no outlet called "view" in it. 你有一个视图控制器笔尖,但它没有名为“视图”的插座。 Otherwise whatever code you have written would work well. 否则你写的任何代码都会很好用。

After so many research finally I found a way to Add it to window . 经过这么多的研究,我终于找到了将它添加到window

Now I can user viewDidLoad and all other methods also. 现在我也可以使用viewDidLoad和所有其他方法。

- (void)showSearchResultsView
{

if(!self.searchDisplayView){

    self.searchDisplayView = [[SearchResultDisplayVC alloc] initWithNibName:@"SearchResultDisplayVC" bundle:nil];

    self.searchDisplayView.delegate = self;


    CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size;

    self.searchDisplayView.view.frame = CGRectMake(0,
                                               0,
                                               mainScreenSize.width,
                                               mainScreenSize.height);
}

AppDelegate *delegate =  (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window addSubview:self.searchDisplayView.view];


 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM