2010-01-02 15 views

Respuesta

17

Aquí es un ejemplo de cómo se puede hacer esto:

- (void)viewDidLoad 
{ 

    //standard UIScrollView is added 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
    [self.view addSubview:scrollView]; 

    scrollView.pagingEnabled = YES; 
    scrollView.contentSize = CGSizeMake(320*2, 460); //this must be the appropriate size! 

    //required to keep your view controllers around 
    controllers = [[NSMutableArray alloc] initWithCapacity:0]; 

    //just adding two controllers 
    LabeledViewController *one = [[LabeledViewController alloc] initWithPosition:0 text:@"one"]; 

    [scrollView addSubview:one.view]; 
    [controllers addObject:one]; 

    LabeledViewController *two = [[LabeledViewController alloc] initWithPosition:1 text:@"two"]; 
    [scrollView addSubview:two.view]; 
    [controllers addObject:two]; 
} 

LabeledViewController es bastante simple, pero puede agregar tanto a él como desee:

@implementation LabeledViewController 

- (id)initWithPosition:(NSInteger)position text:(NSString*)text 
{ 
    if (self = [super init]) { 
     myPosition = position; 
     myText = [text retain]; 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    //this will setup the position in the UIScrollView 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320*myPosition, 0, 320, 460)]; 
    self.view = view; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)]; 
    label.text = myText; 

    [self.view addSubview:label]; 
} 
+0

Sí, usted siempre puede usar básicamente su controlador de vista como una vista a través de su propiedad de vista. viewController.view o [viewController view]. –

+0

¡Gracias por el código! una nota, no debe llamar a [super loadView] en loadView. En su lugar, puede crear una vista al principio UIView * myView = [[UIView alloc] init]; agregue el scrollView a myView y luego al final do self.view = myView –

+1

Esto funciona. Sin embargo, tengo botones en mi vista y me gustaría que el controlador de vista secundaria responda a los botones. Lo conecté correctamente, pero recibo un error como si mi controlador de vista no tuviera los métodos de acción. – SpaceTrucker

Cuestiones relacionadas