4

I am completely new to iPhone development. I have two ViewControllers

  1. ViewControllerA
  2. ViewControllerB

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.

0

6 Answers 6

10

Try this

ViewControllerB *vcb = [[ViewControllerB alloc] init];

[self.view addSubview:vcb.view];
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

This link is dead.
1

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

}

Comments

1

try this

in "viewDidLoad" method of "ViewController1"

ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self addChildViewController: vc2];
[self.view addSubview: vc2.view];

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

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

Comments

0

Add [self.view addSubView:ViewControllerB.view] in the viewDidLoad() of ViewControllerA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.