1

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.'
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • Yes you are adding ViewController as View "`addSubView`". – Bista Jan 09 '16 at 10:15
  • how I can I add it as a subView or is their any other way of doing @the_UB – Rahul Jan 09 '16 at 10:16
  • Check [Micky's and others answer here](http://stackoverflow.com/questions/1486832/how-to-add-an-uiviewcontrollers-view-as-subview). – Bista Jan 09 '16 at 10:20
  • you should check first object you are receiving from nib.i guess that is itself a view instead of your view controller.if thats a view you can simply add it to current view – Minal Soni Jan 09 '16 at 10:37

4 Answers4

1

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

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

Chirag kukreja
  • 433
  • 2
  • 5
1

Please modify your "showSearchResultsView" as:

- (void)showSearchResultsView
 {

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


[self.view addSubview:searchView];

}
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
Minal Soni
  • 607
  • 8
  • 12
1

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.

Satheesh
  • 10,998
  • 6
  • 50
  • 93
0

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


 }
Rahul
  • 5,594
  • 7
  • 38
  • 92