2012-05-03 7 views
2

この質問はここにたくさんありますが、これを動作させることはできません。おそらく、私はUIScrollViewにいくつかのラベルと画像をプログラムで追加しようとしています。プログラムでUIScrollViewにUILcelsViewと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 /ストーリーボードでは動作しませんが、あなたは必ずこと、決してIBで定義されたビューをalloc/initする必要がありますか?すなわち、 'scrollView = [[UIScrollView alloc] init];'は必要ですか? –

+0

いいえ、わかりません。それを試してみるべきだったので、それは私が検討していたものの1つだったからです。それを答えに入れてもらえますか?私はただそれを削除し、それは働いた。 – James

+0

投稿した答え! –

答えて

3

を削除してください。scrollView = [[UIScrollView alloc] init]; IBで作業する場合は、必ずしも必要ではありません(逆効果もあります)。

(実際にはコメントで唯一のアイデアだった - 。;-)上記参照しかし、私は可能な限り高い評価を獲得してみてください)

+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); 
} 
関連する問題