Reputation: 7850
I am completely new to iPhone development. I have two ViewControllers
ViewControllerA is the first one and launches with the app.
I have another ViewControllerB now I want to add view of ViewControllerB as subview to ViewControllerA's view when application launches.
Upvotes: 4
Views: 9646
Reputation: 915
try this
in "viewDidLoad" method of "ViewController1"
ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self addChildViewController: vc2];
[self.view addSubview: vc2.view];
Upvotes: 1
Reputation: 3428
A belated answer. I just wrote some words about my solution for this question. It can be found here: http://blog.nguyenthanhnhon.info/2014/04/how-to-add-viewcontrollernavigationcont.html
Upvotes: 2
Reputation: 1043
You need to declare the object of VC globally .. otherwise you face some issues.
@interface ViewControllerA ()
{
ViewControllerB *viewControllerBObj;
}
-(void) viewDidLoad
{
[super viewDidLoad];
viewControllerBObj = [[ViewControllerB alloc]initWithNibName:@"ViewControllerB" bundle:nil];
[self.view addSubview:viewControllerBObj.view];
}
Upvotes: 1
Reputation: 6402
Add [self.view addSubView:ViewControllerB.view]
in the viewDidLoad()
of ViewControllerA.
Upvotes: 0
Reputation: 26400
Try this
ViewControllerB *vcb = [[ViewControllerB alloc] init];
[self.view addSubview:vcb.view];
Upvotes: 10
Reputation: 46037
You can access the view of a view controller by using it's view
property. If you have pointers to two view controllers myControllerA
and myControllerB
then you can add B's view to A's view by using this.
[myControllerA.view addSubview:myControllerB.view];
Upvotes: 0