Question

i have added a UIViewController's view and a button to scrollview as subview...now how do i release all instance of UIViewController's...here is an attached imagealt text

in the view every uiview has a button over it for further navigation....

now i want to release uiview's and remove buttons from the scrollview ...here is my method to add subviews -(void)AddOneButton:(NSInteger)myButtonTag { lastButtonNumber = lastButtonNumber + 1;

if ((lastButtonNumber == 1) || ((lastButtonNumber%2) == 1)) {
btnLeft = 8;}
else if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) {
btnLeft = 162;
}

 CGRect frame1 = CGRectMake(btnLeft, btnTop, 150, 150);
CGRect frame2 = CGRectMake(btnLeft, btnTop, 150, 150);
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = frame1;
Button.tag = myButtonTag;
[Button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[Button setBackgroundColor:[UIColor clearColor]];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateHighlighted];
[Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateSelected];
if(categoryType==1)
{
    MultiChoiceThumbViewControllerobj = [[MultiChoiceThumbViewController alloc] initWithPageNumber:myButtonTag+1];
    MultiChoiceThumbViewControllerobj.view.frame=frame2;
    MultiChoiceThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
    MultiChoiceThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
    [myScrollView addSubview:MultiChoiceThumbViewControllerobj.view];       
}
[myScrollView addSubview:Button];
 }

with this code subviews count of scrollview is=96.

i have used this method in dealloc to remove subviews but it is not releasing UIvicontroller's

     for(UIView *subview in [myScrollView subviews])
    {
        [subview removeFromSuperview];
        NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
    }

how do i release uiviewcontroller's???

Was it helpful?

Solution

When you created your UIViewControllers you never released your own ownership of it, so until you release it yourself, it will never go away. The problem is that nothing else owns the UIViewControllers, so if you used autorelease, they would be deallocated right away.

My solution would be to create an NSMutableArray and add them to the array before calling release on them. Then, when you want to release all of your UIViewControllers, remove them from the array. NSMutableArray retains ownership of objects you add to it and releases ownership upon removal or deallocation. Something like this:

-(id) init {
    subViewControllers = [NSMutableArray new];
}
...
-(void)AddOneButton:(NSInteger)myButtonTag {
    ...
    if(categoryType==1) {
        MultiChoiceThumbViewControllerobj = [[MultiChoiceThumbViewController alloc] initWithPageNumber:myButtonTag+1];
        MultiChoiceThumbViewControllerobj.view.frame=frame2;
        MultiChoiceThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count];
        MultiChoiceThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag];
        [myScrollView addSubview:MultiChoiceThumbViewControllerobj.view];
        [subViewControllers addObjet:MultiChoiceThumbViewControllerobj];
        [MultiChoiceThumbViewControllerobj release];
    }
    [myScrollView addSubview:Button];
}
...
- (void) removeSuperviews {
    for(UIView *subview in [myScrollView subviews]) {
            [subview removeFromSuperview];
            NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
    }
    [subViewControllers removeAllObjects];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top