2012-06-19 61 views
0

所以我在我看來做了兩個UIscrollview。我正在使用Ray WenderlichCustom Image Picker。但是當我加載它時,只顯示1個imagepicker。我想能夠加載兩個圖像選擇器。我認爲我在initWithCoder部分做錯了。不能似乎正確地初始化它。是否有可能有兩個ivars self。抱歉有點新的iphoneDev。謝謝你的幫助。兩個UIScrollView只出現一個

這裏是我的整個實現:

- (id) initWithCoder:(NSCoder *)aDecoder { 
    if ((self = [super initWithCoder:aDecoder])) { 
     _images = [[NSMutableArray alloc] init]; 
     _thumbs = [[NSMutableArray alloc] init]; 

     //THIS IS WHERE I THINK ITS WRONG but the upper part seems to be okay. 
     _images2 = [[NSMutableArray alloc] init]; 
     _thumbs2 = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

- (void)addImage:(UIImage *)image { 
    [_images addObject:image]; 
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; 
} 
- (void)addImage2:(UIImage *)image { 
    [_images2 addObject:image]; 
    [_thumbs2 addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; 
} 


- (void) createScrollView { 
    self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 300, 143)]; 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.slotBg.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 
    [self.slotBg.layer insertSublayer:gradient atIndex:0]; 
    [self.view addSubview:self.slotBg]; 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)]; 
    [slotBg addSubview:scrollView]; 

    int row = 0; 
    int column = 0; 
    for(int i = 0; i < _thumbs.count; ++i) { 

     UIImage *thumb = [_thumbs objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 

     [scrollView addSubview:button]; 

     if (column == 4) { 
      column = 0; 
      row++; 
     } else { 
      column++; 
     } 

    } 

    [scrollView setContentSize:CGSizeMake(330, (row+1) * 60 + 10)]; 
} 

- (void) createScrollView2 { 
    self.slotBg2 = [[UIView alloc] initWithFrame:CGRectMake(362, 370, 300, 143)]; 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = self.slotBg.bounds; 
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]; 
    [self.slotBg.layer insertSublayer:gradient atIndex:0]; 
    [self.view addSubview:self.slotBg]; 

    UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)]; 
    [slotBg addSubview:scrollView2]; 

    int row = 0; 
    int column = 0; 
    for(int i = 0; i < _thumbs2.count; ++i) { 

     UIImage *thumb = [_thumbs2 objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); 
     [button setImage:thumb forState:UIControlStateNormal]; 
     [button addTarget:self 
        action:@selector(buttonClicked2:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     button.tag = i; 

     [scrollView2 addSubview:button]; 

     if (column == 4) { 
      column = 0; 
      row++; 
     } else { 
      column++; 
     } 

    } 

    [scrollView2 setContentSize:CGSizeMake(330, (row+1) * 60 + 10)]; 

} 



- (IBAction)buttonClicked:(id)sender { 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSInteger slotBG = [prefs integerForKey:@"integerKey"]; 

    if(slotBG == 1){ 
    UIButton *button = (UIButton *)sender; 
    [button removeFromSuperview]; 
    [_images objectAtIndex:button.tag]; 
    [_images removeObjectAtIndex:button.tag]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]]; 
    [fileManager removeItemAtPath: fullPath error:NULL]; 
    NSLog(@"image removed"); 
    } else if (slotBG == 2){ 
     UIButton *button = (UIButton *)sender; 
     [button removeFromSuperview]; 
     [_images objectAtIndex:button.tag]; 
     [_images removeObjectAtIndex:button.tag]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"firstSlotImages%lu.png", button.tag]]; 
     [fileManager removeItemAtPath: fullPath error:NULL]; 
     NSLog(@"image removed"); 

    } else if (slotBG == 3){ 
     UIButton *button = (UIButton *)sender; 
     [button removeFromSuperview]; 
     [_images objectAtIndex:button.tag]; 
     [_images removeObjectAtIndex:button.tag]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]]; 
     [fileManager removeItemAtPath: fullPath error:NULL]; 
     NSLog(@"image removed"); 
    } 
} 

- (IBAction)buttonClicked2:(id)sender { 
    UIButton *button2 = (UIButton *)sender; 
    [button2 removeFromSuperview]; 
    [_images2 objectAtIndex:button2.tag]; 
    [_images2 removeObjectAtIndex:button2.tag]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"secondSlotImages%lu.png", button2.tag]]; 
    [fileManager removeItemAtPath: fullPath error:NULL]; 
    NSLog(@"image removed"); 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSInteger slotBG = [prefs integerForKey:@"integerKey"]; 

    if(slotBG == 1){ 
     [mode1 setHighlighted:YES]; 

     for(int i = 0; i <= 100; i++) 
     { 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDir = [paths objectAtIndex:0]; 

      NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
      NSLog(@"savedImagePath=%@",savedImagePath); 
      if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
       [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 

       NSLog(@"file exists"); 
      } 
     } 
      NSLog(@"Count : %d", [_images count]); 
     [self createScrollView]; 

    } else if(slotBG == 2){ 
     [mode2 setHighlighted:YES]; 
     for(int i = 0; i <= 100; i++) 
     { 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDir = [paths objectAtIndex:0]; 

      NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"firstSlotImages%d.png", i]]; 
      NSLog(@"savedImagePath=%@",savedImagePath); 
      if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
       [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 

       NSLog(@"file exists"); 
      } 
     } 
      NSLog(@"Count : %d", [_images count]); 

      [self createScrollView]; 

     for(int i = 0; i <= 100; i++) 
     { 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDir = [paths objectAtIndex:0]; 

      NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"secondSlotImages%d.png", i]]; 
      NSLog(@"savedImagePath=%@",savedImagePath); 
      if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
       [self addImage2:[UIImage imageWithContentsOfFile:savedImagePath]]; 
       NSLog(@"file exists"); 
      } 
     } 
     NSLog(@"Count : %d", [_images2 count]); 

     [self createScrollView2]; 


    } else if(slotBG == 3){ 
     [mode3 setHighlighted:YES]; 

     for(int i = 0; i <= 100; i++) 
     { 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDir = [paths objectAtIndex:0]; 

      NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
      NSLog(@"savedImagePath=%@",savedImagePath); 
      if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
       [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 

       NSLog(@"file exists"); 
      } 
     } 
     NSLog(@"Count : %d", [_images count]); 
     [self createScrollView]; 

    }  




} 

回答

0

在你createScrollView2方法,你ALLOC self.slotBg2,但隨後只添加到您的視圖時引用self.slotBg。然後,將您的scrollview2添加到原始的slotBg。

+0

我的slotBg2在我的.h中設置 – Bazinga

+1

在你的createScrollView2方法中,你只需要引用slotBg2和scrollview2,但是,你仍然在使用slotBg,所以第二個滾動視圖插入到第一個滾動視圖的頂部。 – danielbeard

+0

哦,我明白了。等待我會嘗試在我的代碼中重新編輯。 – Bazinga

相關問題