Ankit Sachan
Ankit Sachan

Reputation: 7850

adding a viewControllers view to another viewController

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.

Upvotes: 4

Views: 9646

Answers (6)

Karan Champaneri
Karan Champaneri

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

Thanh-Nhon Nguyen
Thanh-Nhon Nguyen

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

AP_
AP_

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

PgmFreek
PgmFreek

Reputation: 6402

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

Upvotes: 0

visakh7
visakh7

Reputation: 26400

Try this

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

[self.view addSubview:vcb.view];

Upvotes: 10

taskinoor
taskinoor

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

Related Questions