0

I'm new on iOS development and i got a trouble want to ask you.

My purpose is show a view (not full screen) upon base view and still see a parent on the back outside the subview. So i added a subview (subclass of UIViewController) upon current viewController, then i do to open the subview:

AViewController *a = [[AViewController alloc] initWithNibName:nil bundle:nil];
[self.view addSubView:a.view];
[a release];

Here it works and show this subview as i expected. On this subview, i have close button to close this subview and do like this on subview:

- (IBAction)BackTouchUp:(id)sender 
{
    [self.view removeFromSuperview];
}

When i click to close button, i got an error EXC_BAD_ACCESS. When i remove [a release] command on the parent, it works, but there is a memory leak.

Can you please explain for me the cause of this error and how to resolve this case?

hieund
  • 218
  • 3
  • 6

2 Answers2

0

you can set the a view tag no

[a.view setTag:3];
uiView *f=[self.view viewWithTag:3];
[f removeFromSuperview];

i think this will solve your problem

Mustafa Ibrahim
  • 1,110
  • 1
  • 9
  • 17
0

Define AViewController object (AViewController *a) in interface and in the implementation, initialize the value like below:

a = [[AViewController alloc] initWithNibName:nil bundle:nil];

[self.view addSubView:a.view];

and whenever you want to remove simply put [a removeFromSuperview] and in dealloc

[a release];
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
Mangesh
  • 2,257
  • 4
  • 24
  • 51
  • Thank you for answer. As you said, a is instance variable of parent, isn't it? But [a removeFromSuperview] is in the child view's code, how can i access to a variable of parent? – hieund Apr 11 '12 at 06:48
  • via delegate method, Create custom delegate or Post Notification for the same. – Mangesh Apr 12 '12 at 06:33