以编程方式将UILabels和UIImageView添加到UIScrollView

以编程方式将UILabels和UIImageView添加到UIScrollView,uiscrollview,uiimageview,uilabel,addsubview,Uiscrollview,Uiimageview,Uilabel,Addsubview,我知道这个问题在这里讨论了很多,但我似乎仍然无法让它发挥作用。我可能没有正确启动视图或其他什么。。。无论如何,我正在尝试以编程方式向UIScrollView添加几个标签和图像。以下是我的.h文件的代码: #import <UIKit/UIKit.h> @interface DOR_HelpViewController : UIViewController <UIScrollViewDelegate> { IBOutlet UIScrollView *scroll

我知道这个问题在这里讨论了很多,但我似乎仍然无法让它发挥作用。我可能没有正确启动视图或其他什么。。。无论如何,我正在尝试以编程方式向UIScrollView添加几个标签和图像。以下是我的.h文件的代码:

#import <UIKit/UIKit.h>

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

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

@end

scrollView
链接到我的故事板中的我的
UIScrollView
对象。如果你不介意的话,我将非常感谢你告诉我我做错了什么以及为什么。提前感谢~

删除
scrollView=[[UIScrollView alloc]init]在使用IB时,这是不必要的(甚至会适得其反)

(事实上,这只是评论中的一个想法-见上文。但我尽可能地赢得声誉;-)

-(void)addImagesToScrollView:(NSMutableArray*)jsonArray{
如果(!jsonArray | | jsonArray.count==0){
返回;
}
int-imageXPos=0;
int imageWidth=50;
int-imageHeight=50;

对于(int index=0;indexI从不使用IB/故事板,但您确定必须分配/初始化IB中定义的视图吗?即is
scrollView=[[UIScrollView alloc]init]有必要吗?不,我不确定。因为我应该试一下,因为那是我正在考虑的事情之一。你能把它作为一个答案吗?我刚把它去掉,它就起作用了。
#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
-(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);
}