2012-08-17 60 views
0

我使用此代碼來顯示滾動圖像,但我只是得到一個白色的屏幕。我拖放的UIScrollView,創建IBOutlet中,合成它和代碼的其餘部分是在這裏爲什麼圖像不顯示在UIScrollView中?

@synthesize scrollView1; 

const CGFloat kScrollObjHeight = 199.0; 
const CGFloat kScrollObjWidth = 280.0; 
const NSUInteger kNumImages  = 5; 

- (void)layoutScrollImages 
{ 
    UIImageView *view = nil; 
    NSArray *subviews = [scrollView1 subviews]; 

    // reposition all image subviews in a horizontal serial fashion 
    CGFloat curXLoc = 0; 
    for (view in subviews) 
    { 
     if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
     { 
      CGRect frame = view.frame; 
      frame.origin = CGPointMake(curXLoc, 0); 
      view.frame = frame; 

      curXLoc += (kScrollObjWidth); 
     } 
    } 

    // set the content size so it can be scrollable 
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    scrollView1 = [[UIScrollView alloc]init]; 

    bgArray = [NSArray arrayWithObjects:@"Bg", @"Bg1.png", @"Bg2.png", @"Bg3.png", @"Bg4.png", @"Bg5.png", @"Bg6.png", @"Bg7.png", @"Bg8.png", nil]; 

    [scrollView1 setBackgroundColor:[UIColor blackColor]]; 
    [scrollView1 setCanCancelContentTouches:NO]; 
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    scrollView1.scrollEnabled = YES; 

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo 
    // if you want free-flowing scroll, don't set this property. 
    scrollView1.pagingEnabled = YES; 

    // load all the images from our bundle and add them to the scroll view 
    NSUInteger i; 
    for (i = 0; i < [bgArray count]; i++) 
    { 
     NSString *imageName = [NSString stringWithFormat:@"%@", [bgArray objectAtIndex:i]]; 
     UIImage *image = [UIImage imageNamed:imageName]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

     // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
     CGRect rect = imageView.frame; 
     rect.size.height = 199.0; 
     rect.size.width = 280.0; 
     imageView.frame = rect; 
     imageView.tag = i; // tag our images for later use when we place them in serial fashion 
     [scrollView1 addSubview:imageView]; 
     [scrollView1 setShowsHorizontalScrollIndicator:NO]; 
     [scrollView1 setShowsVerticalScrollIndicator:NO]; 
    } 

    [self layoutScrollImages]; 

} 

回答

3

我認爲,如果你是從代碼中創建UIScrollView

scrollView1 = [[UIScrollView alloc]init]; 

那麼請加入scrollView1鑑於 例: - [self.view addSubview:scrollView1];

,如果你是從筆尖創建的UIScrollView然後 不使用此代碼

scrollView1 = [[UIScrollView alloc]init]; 

解決您的probem

+0

我加入的UIScrollView從IB,但沒有的alloc初始化其零 – 2012-08-17 08:19:55

+0

是,請連接從fileOwner和委託使用的UIScrollView UIScrollView的連接到fileOwner在筆尖 – Deepesh 2012-08-17 09:50:21