views:

734

answers:

2

Help me. addSubview doesn't work. I want to add "ContentView" on scrollView. But "ContentView" doesn't appear on screen. "ContentView" is UIView.

When I change to self.view=scrollView from [self.view addSubview:scrollView], addSubview doesn't work.

Please teach me how to add UIView on this screen!!

- (void)viewDidLoad {
[super viewDidLoad];

scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init]; 
mainStatic = [[MainStatics alloc] init];    
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:@"Files" ofType:@"plist"];      
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];    
kNumImages = [dataFiles count]; 

CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];

NSUInteger i;                                                   
for (i=0;i<[dataFiles count];i++)
{
    UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;                
    imageView.userInteractionEnabled = NO;                                  
    CGRect rect = imageView.frame;                                          
    rect.size.height = 480;                                 
    rect.size.width = 320;                                      
    imageView.frame = rect;                                                 
    imageView.tag = i+1;                                                                       
            [imageViewArray addObject:imageView];
}

self.view = scrollView;

[self setPagingMode];
UIView* contentView;                                            
CGRect scRect = CGRectMake(0, 436, 320, 44);                    
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];                              
[self addSubview:contentView];                              

}

A: 

Source isn't clear. You never use array with UIImageView. There is no info what is toolView etc... Please provide more detailed source. Is touchClass a subclass of UIScrollView?

What I noticed in your code:

  1. You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
  2. I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];
OgreSwamp
Thank you.Please check my new comment.
A: 

I used image array in following function. toolView is UIView with UIToolbar. So I want to add toolbar to screen. Yes, touchClass is subclass of UIScrollView. 1.I see.I forgot release this.Thank you. 2.OK. I modified this.But "ContentView" didn't apear.

are there another reason?

- (void)setPagingMode {

    CGSize pageSize = [self pageSize];
    NSUInteger page = 0;
    for (UIView *view in imageViewArray){
        [scrollView addSubview:view];
        view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
    }

    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
    scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);    
    scrollViewMode = ScrollViewModePaging;
}