Rahul
Rahul

Reputation: 5834

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.'

Upvotes: 1

Views: 366

Answers (4)

Minal Soni
Minal Soni

Reputation: 627

Please modify your "showSearchResultsView" as:

- (void)showSearchResultsView
 {

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


[self.view addSubview:searchView];

}

Upvotes: 1

Rahul
Rahul

Reputation: 5834

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


 }

Upvotes: 0

Satheesh
Satheesh

Reputation: 11276

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,

enter image description here

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.

Upvotes: 1

Chirag kukreja
Chirag kukreja

Reputation: 433

you have to add as child view controller in the parent view.

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

Upvotes: 1

Related Questions