Добавяне на UILabels и UIImageViews към UIScrollView програмно

Знам, че този въпрос е тук често, но все още не мога да го накарам да работи. Вероятно не инициирам изгледа правилно или нещо подобно... Както и да е, опитвам се програмно да добавя няколко етикета и изображения към 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 обект в моята сценария. Ще съм много благодарен за информация какво правя грешно и защо, ако нямате нищо против. Благодаря предварително~


person James    schedule 03.05.2012    source източник
comment
Никога не работя с IB/сторибордове, но сигурни ли сте, че трябва да разпределите/инициирате изглед, дефиниран в IB? т.е. необходимо ли е scrollView = [[UIScrollView alloc] init];?   -  person Kai Huppmann    schedule 03.05.2012
comment
Не, не съм сигурен. Тъй като трябваше да опитам това, защото това беше едно от нещата, които обмислях. Можете ли да поставите това като отговор? Просто го махнах и проработи.   -  person James    schedule 03.05.2012


Отговори (1)


Премахване scrollView = [[UIScrollView alloc] init]; Не е необходимо (дори контрапродуктивно) при работа с IB.

(всъщност това беше само идея в коментар - вижте по-горе. Но се опитвам да спечеля репутация, когато е възможно ;-) )

person Kai Huppmann    schedule 03.05.2012