Remove UIViewController from UIScrollView?

Posted by tobi on Stack Overflow See other posts from Stack Overflow or by tobi
Published on 2010-06-01T17:23:55Z Indexed on 2010/06/01 21:03 UTC
Read the original article Hit count: 369

Hi, i add different View with (setPageID) to a ScrollView, but know i get a Memory problem on rotaion and i want to remove the actualy not showed view... how can i do this or how can i remove the memory problem?

Thanks!!!

    - (void)setPageID:(int)page
    {
        if (page < 0) return;
        if (page >= self.listOfItems.count) return;

        CGFloat cx = 0;

        ScrollingViewStep *controller = [viewControllers objectAtIndex:page];
        if ((NSNull *)controller == [NSNull null]) {
            controller = [[ScrollingViewStep alloc] init];          
            [viewControllers replaceObjectAtIndex:page withObject:controller];
            [controller release];
        }

        if (nil == controller.view.superview) {

            if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
            {
                cx = 768.0 * page;
                controller.view.frame = CGRectMake(cx, 0.0 , 768.0f, 926.0f);
            } 
            else
            {
                cx = 1024.0 * page;
                controller.view.frame = CGRectMake(cx, 0.0 , 1024.0f, 670.0f);
            }

            [controller setView:ItemID PageID:page Text:[[self.listOfItems objectAtIndex:page] objectForKey:@"step"]];
            [scrollView addSubview:controller.view];
        }



    }



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    CGFloat cx2 = 0;

    for (int i = 0; i < [self.viewControllers count]; i++) {
        ScrollingViewStep *viewController = [self.viewControllers objectAtIndex:i];
        if ((NSNull *)viewController != [NSNull null]) 
        {
            if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
            {
                cx2 = 768.0 * i;
                viewController.view.frame = CGRectMake(cx2, 0.0 , 768.0f, 926.0f);
                [viewController repos];

            }
            else
            {
                cx2 = 1024.0 * i;
                viewController.view.frame = CGRectMake(cx2, 0.0 , 1024.0f, 670.0f);
                [viewController repos];


            }
        }
    }
    if((interfaceOrientation == UIInterfaceOrientationPortrait) ||
       (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {    
        CGRect frame = scrollView.frame;
        frame.origin.x = 768 * currentPageInScrollview;
        frame.origin.y = 0;
        [scrollView scrollRectToVisible:frame animated:NO];



    }
    else {
        CGRect frame = scrollView.frame;
        frame.origin.x = 1024 * currentPageInScrollview;
        [scrollView scrollRectToVisible:frame animated:NO];

    }

    pageControlIsChangingPage = YES;
    return YES;
}

- (void)didReceiveMemoryWarning {

    int currentPage = currentPageInScrollview;
    NSLog(@"MEMORY");
    // unload the views+controllers which are no longer visible
    UIViewController *l;
    for (int i = 0; i < [self.viewControllers count]; i++) {
        ScrollingViewStep* viewController = [self.viewControllers objectAtIndex:i];
        if((NSNull *)viewController != [NSNull null])
        {
            if(i < currentPage-1 || i > currentPage+1)
            {
                [self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
            }
        }

    }
    [super didReceiveMemoryWarning];


}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ipad