I want to load a view dynamically on click of a button.
Lets say view is ‘MyView’
I declared it as
IBOutlet UIView *myView;
and connected it via interface builder from a myView.xib made.
I did not set property and synthesize for this view
Now what i want is to allocate this view a memory on click of a button
so i tried in button pressed method of my viewController
(void) buttonPressed1{
myView = myView = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0];
myView.frame = CGRectMake(30, 40, 200, 200);
[self.view addSubview:myView];
}
It was Successfully loaded. 🙂
Now i want to access this view in other button click..
so i wrote
-(IBAction) buttonPressed2:(id)sender
{
myView.alpha = 0;
[myView release];
}
so when i run this —it works ok
on buttonPressed it adds that view on other click it fades out and releases that view.
But now when i again click on button to add view it says
modifying layer that is being finalized - 0x6807b60
then on clicking button 2 , it crashes…
Basically my requirement is to keep that view in memory only when it is used after it has been used, it should be deallocated..
so is above approach is good one or there is any other way to do it…
and what does that error mean????
! more query is
Here in above case….
Memory to myView is allocated at run time on click of button ????….
As when i am testing this via instrument in xcode, there is no allocations showing in graph on button click…
so please help me out…
Please help..
Thanks in advance
When you are releasing view :
[myView release];it is not removed from superview. Moreover : you should not release it as it was created as autoreleased object.The solution could be next: