I want to add several ViewController
's views as sub views of my another viewcontroller
. So I have done like this inside my parent viewController
.
-(void)MakeDisplayArea
{
vwDisplayArea=[[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h-scrolTab.frame.size.height)];
[self.view addSubview:vwDisplayArea];
}
-(void)ProfClick :(id)sender
{
[self MakeDisplayArea];
ProfileViewController *profviewcontroller=[[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
profviewcontroller.view.frame=vwDisplayArea.frame;
[profviewcontroller.view setBackgroundColor:[UIColor clearColor]];
[vwDisplayArea addSubview:profviewcontroller.view];
}
Then inside the profViewcontroller
ViewDidLoad
methods I am generating a button and set the target like this.
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(10, 60, 100, 30)];
[btn setTitle:@"Button Test" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)btnClick :(id)sender
{
NSLog(@"button clicked-------");
}
I can see the button is appearing on that profViewcontroller
that I loaded as a sub view, but when I click the button the app is crashing. What is the correct way add that viewcontroller
as a subview
on my first viewcontroller
. Please help me.
Thanks