I am working on a Tab Bar based application
I have a class A with its .xib and a class B with its .xib
On class A i am loading multiple instances of class B.
i.e In class A ,i am doing.
in .m file
-(void)renderData:(NSArray *)Data
{
for(int i=0;i<[Data count];i++)
{
B *grid=[[B alloc]initWithNibName:@"Beta" bundle:[NSBundle mainBundle]];
.
.
. //do something with the grid object i.e assign new image etc..)
[self.myGrid addObject:grid]; //i have a NSMutableArray myGrid declared in .h
[grid release];
}
}
now in the myGrid Array i have all the objects of the grid saved.
Now i am adding them to the class Aplha view.
for(int i=0;i<[myGrid count];i++)
{
B *grid1=[[myGrid objectAtIndex:i]retain]; //i have done this retain because when i try to come back to this tab or try to call the same function it crashes by saying message send to deallocated instance.
[self.view addSubview:grid1.view];
}
now my problem is that how to release the grid1 object that i have retained.
You are approaching this wrong. The problem here isn’t how to release the
grid1object, it’s why you are retaining them in the first place. You most likely shouldn’t be; you need to investigate the original crash more thoroughly.If your
grid1objects are stored inself.myGridthen they are retained by that array. Are you releasingmyGridanywhere? As long as that is retained, yourgrid1objects should be.In addition, there are some conceptual issues here. Loading a view controller from a nib and adding it’s view as a sub-view of another view controller’s view is generally not correct. It’s hard to recommend the correct approach without knowing exactly what you are trying to achieve though.
You do not need to pass in
[NSBundle mainBundle]toinitWithNibName:bundle:– you can simply pass in nil as the default behaviour is to use the main bundle.Your comment says you have “assigned” an
NSMutableArrayin your header. You don’t assign anything in your header, you just declare things. Have you actually initialised theNSMutableArraysomewhere in your implementation?