Implementando UIScrollView programáticamente

En elpage control sample desde apple hay un ScrollView en el generador de interfaces. Está vinculado con el IBOutlet correspondiente. Quiero cambiar el código para que todo esto se realice programáticamente. Elimino el objeto generador de interfaz, elimino la palabra clave IBOutlet. Asigno e inicio el scrollView, pero no aparece nada cuando ejecuto el programa.

Supongo que esto se debe a que necesito asignarlo como una subvista a la vista principal. O yo? Todavía no entiendo cómo funcionan e interactúan todas las vistas entre sí. Si lo hago[self.view addSubView:ScrollView]; Recibo un error de tiempo de ejecución (o algo así, por lo general solo dice algo como BAD ACCESS o SIGABRT).

¿Qué estoy haciendo mal? ¿Estoy en el camino equivocado por completo? (solo dos días para la programación de ios, todavía un poco perdido en el bosque)

awakeFromNib en el controlador de contenido del teléfono:

- (void)awakeFromNib
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.si,ze.height)];

// load our data from a plist file inside our app bundle
NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"];
self.contentList = [NSArray arrayWithContentsOfFile:path];

// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++)
{
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];

// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages,  scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;

pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;

// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
//
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}

archivo de cabecera

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

#import "ContentController.h"

@interface PhoneContentController : ContentController <UIScrollViewDelegate>
{   
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;

// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}

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

@property (nonatomic, retain) NSMutableArray *viewControllers;

- (IBAction)changePage:(id)sender;

@end

appDelegate:

#import "AppDelegate.h"
#import "ContentController.h"

@implementation AppDelegate

@synthesize window, contentController;

- (void)dealloc
{
[window release];
[contentController release];

[super dealloc];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSString *nibTitle = @"PadContent";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    nibTitle = @"PhoneContent";
}
[[NSBundle mainBundle] loadNibNamed:nibTitle owner:self options:nil];

[self.window addSubview:self.contentController.view];
[window makeKeyAndVisible];
}

@end

y scrollView se ha eliminado del archivo xib. Nota: esta es una nueva versión del programa descargado donde todo lo que he cambiado es eliminar la palabra clave IBOutlet para scrollView, eliminar el scroll de xib y agregar el alloc, init line en estado de alerta de nib.

He tenido sugerencias para cambiar el appDelegate y cambiar el wakekefromNib a un método init, he intentado todo esto pero aún no funciona.

Respuestas a la pregunta(6)

Su respuesta a la pregunta