2012-05-03 3 views
2

이 질문은 여기에 많이 있지만, 여전히 작동하지 않는 것 같습니다. 아마 올바르게 또는 뭔가보기를 시작하지 않을거야 ... 어쨌든, 나는 UIScrollView에 프로그래밍 방식으로 여러 레이블과 이미지를 추가하려고합니다.프로그래밍 방식으로 UIScrollView에 UILabels 및 UIImageViews 추가

#import <UIKit/UIKit.h> 

@interface DOR_HelpViewController : UIViewController <UIScrollViewDelegate> { 
    IBOutlet UIScrollView *scrollView; 
} 

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView; 

@end 

그리고 내하는 .m 파일 :

#import "DOR_HelpViewController.h" 

@implementation DOR_HelpViewController 

@synthesize scrollView; 

- (void)viewWillAppear:(BOOL)animated {  

    [super viewWillAppear:animated]; 

    scrollView = [[UIScrollView alloc] init]; 

    UILabel *pointsCouponLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 320.0, 15.0)]; 
    pointsCouponLbl.font = [UIFont boldSystemFontOfSize:14.0]; 
    pointsCouponLbl.textAlignment = UITextAlignmentCenter; 
    pointsCouponLbl.textColor = [UIColor blackColor]; 
    pointsCouponLbl.backgroundColor = [UIColor clearColor]; 
    pointsCouponLbl.text = @"Points Earned Using a Coupon"; 
    [scrollView addSubview:pointsCouponLbl]; 

    UIImageView *pointsCouponImg = [[UIImageView alloc] initWithFrame:CGRectMake(72, 45, 175, 100)]; 
    pointsCouponImg.image = [UIImage imageNamed:@"couponpoints.png"]; 
    [scrollView addSubview:pointsCouponImg]; 

    UILabel *pointsCheckInLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 165.0, 320.0, 15.0)]; 
    pointsCheckInLbl.font = [UIFont boldSystemFontOfSize:14.0]; 
    pointsCheckInLbl.textAlignment = UITextAlignmentCenter; 
    pointsCheckInLbl.textColor = [UIColor blackColor]; 
    pointsCheckInLbl.backgroundColor = [UIColor clearColor]; 
    pointsCheckInLbl.text = @"Points Earned For Check-In"; 
    [scrollView addSubview:pointsCheckInLbl]; 
    pointsCheckInLbl = nil; 

    UIImageView *pointsCheckInImg = [[UIImageView alloc] initWithFrame:CGRectMake(72, 190, 175, 100)]; 
    pointsCheckInImg.image = [UIImage imageNamed:@"checkinpoints.png"]; 
    [scrollView addSubview:pointsCheckInImg]; 
    pointsCheckInImg = nil; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

scrollView은 내 스토리 보드 내 UIScrollView 객체에 연결되어 여기 내 .H 파일에 대한 내 코드입니다. 나는 내가 잘못하고있는 것에 대한 정보를 매우 감사 할 것이고, 만약 당신이 신경 쓰지 않는다면 왜 그렇게 할 것인가. 미리 감사드립니다 ~

+0

, 그러나 확신하는 당신이 IB에 정의 된 뷰를 할당/초기화해야합니까? 즉,'scrollView = [[UIScrollView alloc] init];'필요한가? –

+0

아니요, 확실하지 않습니다. 내가 그것을 시험해보아야했기 때문에 그것이 내가 고려하고 있었던 것들 중 하나이기 때문이다. 그걸 답으로 써 주시겠습니까? 방금 제거하고 작동했습니다. – James

+0

게시 됨 내 대답! –

답변

3

제거 scrollView = [[UIScrollView alloc] init]; IB로 ​​작업 할 때 필요하지 않습니다 (비생산적 임).

는 (. 실제로는 코멘트 만 생각했다 - 위 참조하지만 가능한 한 ;-) 명성을 얻기 위해 시도) IB/스토리 보드와 함께 작동하지 I

+0

다시 한번 감사드립니다. 그것은 모두 좋은, 여전히 내 문제를 해결. :) – James

0
-(void)addImagesToScrollView:(NSMutableArray*)jsonArray{ 


if (!jsonArray || jsonArray.count ==0) { 
    return; 
} 

int imageXPos=0; 

int imageWidth=50; 
int imageHeight=50; 

for (int index=0; index<[jsonArray count]; index++) 
{ 
    NSMutableDictionary *dict=[jsonArray objectAtIndex:index]; 
    NSString *thumbUrl=[JsonUtil getString:dict forKey:@"thumb"]; 

    UIImageView *imageView = [[UIImageView alloc] init ]; 
    [imageView setImageWithURL:[NSURL URLWithString:thumbUrl] 
       placeholderImage:[UIImage imageNamed:@"place_image"]]; 
    imageView.contentMode = UIViewContentModeScaleToFill; 
    imageView.clipsToBounds = YES; 

    imageView.frame = CGRectMake(imageXPos,1, imageWidth, imageHeight); 

    [self.placeImageScrollView addSubview:imageView]; 

    imageXPos = imageXPos + imageView.frame.size.width +2;//2 is padding 

} 
self.placeImageScrollView.contentSize = CGSizeMake(imageXPos, self.placeImageScrollView.frame.size.height); 
} 
관련 문제