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

时间:2012-05-03 15:00:55

标签: uiscrollview uiimageview uilabel addsubview

我知道这个问题在这里很多,但我似乎仍然无法让这个工作。我可能没有正确地启动视图或者某些东西......无论如何,我正在尝试以编程方式向UIScrollView添加几个标签和图像。这是我的.h文件的代码:

#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对象相关联。我非常感谢有关我做错的信息,以及为什么如果你不介意的话。提前谢谢〜

2 个答案:

答案 0 :(得分:3)

删除scrollView = [[UIScrollView alloc] init];使用IB时没有必要(甚至适得其反)。

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

答案 1 :(得分:0)

a