user440096
user440096

Reputation:

Converting a UIViewController to a UIView

There is a class that's a fully fledged UIViewController and when that page loads it works very nice.

I want to change how it works and rather than push on a new ViewController form my current viewcontroller, I'd rather just show this UIViewController inside a UIView on the current screen that usually launchs the new UIViewController.

This is hard to explain.

UIVC_A pushes UIVC_B onto the NavController.

Instead of having UIVC_B getting pushed onto the NavController.

I'd rather creat a UIView in UIVC_A and have UIVC_B appear inside this as a subview of UIVC_A.

What would I need to do/change to get this kind of thing working.

I tried

CVC *cv = [[CVC alloc] init];    
UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 600, 600)];
aView.backgroundColor = [UIColor blueColor];
[aView addSubview:cv.view];
[self.view addSubview:aView];

But it did not work.

CVC is a UIViewController.

Thanks, -Code

Upvotes: 0

Views: 1535

Answers (2)

Balakrishnan Mca
Balakrishnan Mca

Reputation: 747

Add this function to your CVC view controller

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

and try with this code

CVC *cv = [[CVC alloc] initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    
UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 600, 600)];
aView.backgroundColor = [UIColor blueColor];
[aView addSubview:cv.view];
[self.view addSubview:aView];

this code is working fine for me I think you too, good luck!!!.

Upvotes: 1

Eugene
Eugene

Reputation: 10045

It didn't work because the CVC's xib file hasn't unarchived yet. It's view hierarchy is available for usage after it has been loaded (the callback is -viewDidLoad).

What you need to do is programmatically load nib and use it's contents:

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CVC" owner:nil options:nil]; // will return the whole view tree with the CVC's view as it's 0 object
UIView *cvcView = [topLevelObjects objectAtIndex:0];

and proceed as you did with the view.

Upvotes: 0

Related Questions