简体   繁体   中英

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 .

Now I have another ViewController With XIB . All I want is to present This XIB as a View in my Parent Class.

Name of My ViewController With XIB is: SearchResultDisplayVC

This code I am using my Base Class to Show My XIB as a View:

 - (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"

[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:

- (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. Like below,

在此输入图像描述

All view controllers expect a "view" object to be defined and mapped from nib files. 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 .

Now I can user viewDidLoad and all other methods also.

- (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];


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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