adding a viewControllers view to another viewController
I am completely new to iPhone development. I have two ViewControllers
- ViewControl开发者_JAVA百科lerA
- 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.
Try this
ViewControllerB *vcb = [[ViewControllerB alloc] init];
[self.view addSubview:vcb.view];
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
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];
}
try this
in "viewDidLoad" method of "ViewController1"
ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self addChildViewController: vc2];
[self.view addSubview: vc2.view];
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];
Add [self.view addSubView:ViewControllerB.view]
in the viewDidLoad()
of ViewControllerA.
精彩评论