-1

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

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
IRD
  • 1,127
  • 2
  • 8
  • 15

1 Answers1

0

Like maddy said you need child view controllers to add working subviews.

Look here: Creating Custom Container View Controllers

But working with them is not easy. Ask yourself if you really need to implement it. For the most cases you don't need it.

Jonas
  • 2,139
  • 17
  • 38