2010-06-07 10 views
5

Creo una vista personalizada en cocoa touch que está superclasificada por UIView y en mi controlador principal la inicializo y luego la agrego como subvista a la vista principal, pero cuando la agrego a la vista principal llama a mi método de inicialización de nuevo y causa un bucle infinito. ¿Voy a crear mal mi vista personalizada? Aquí es el MAINVIEWCocoa Touch: Crear y agregar una vista personalizada

- (void)loadView { 
    UIImage* tempImage = [UIImage imageNamed: @"image1.jpg"]; 
    CustomImageContainer *testImage = [[CustomImageContainer alloc] initWithImage: tempImage andLabel: @"test image" onTop: true atX: 10 atY: 10]; 
    [self.view addSubview: testImage]; 
} 

y la CustomImageContainer

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{ 
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd]; 
    imageview_to_add.frame = CGRectMake(0, 0, imageToAdd.size.width, imageToAdd.size.height); 
    UILabel *label_to_add = [[UILabel alloc] init]; 
    label_to_add.text = text; 
    label_to_add.alpha = 50; 
    label_to_add.backgroundColor = [UIColor blackColor]; 
    label_to_add.textColor = [UIColor whiteColor]; 
    [self addSubview: imageview_to_add]; 
    self.frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height); 
    if (top) { 
     label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height); 
     //[self addSubview: label_to_add]; 
    } 
    else { 
     label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height); 
    } 
    [self addSubview: label_to_add]; 
    [super init]; 
    return self; 
} 

Respuesta

1

¿Por qué se pone la declaración [super init] al final de la inicialización? Cuando subclases, usualmente colocas esta declaración al comienzo del método.

Para las subclases UIView, el inicializador designado al crear vistas en código es initWithFrame:, por lo que debe llamarlo antes de agregar la etiqueta y la imagen. Puede usar la imagen para calcular el cuadro que necesita la vista personalizada.

-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{ 
    // The view will gets its frame to the size of the image 
    UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd]; 

    // Call the designated initializer 
    CGRect frame = CGRectMake(x_cord, y_cord, imageToAdd.size.width, imageToAdd.size.height); 
    self = [super initWithFrame:frame]; 

    [self addSubview: imageview_to_add]; 

    UILabel *label_to_add = [[UILabel alloc] init]; 
    label_to_add.text = text; 
    label_to_add.alpha = 50; 
    label_to_add.backgroundColor = [UIColor blackColor]; 
    label_to_add.textColor = [UIColor whiteColor]; 

    if (top) { 
     label_to_add.frame = CGRectMake(0, 0, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height); 
    } 
    else { 
     label_to_add.frame = CGRectMake(0,.2 * imageview_to_add.frame.size.height, imageview_to_add.frame.size.width, imageview_to_add.frame.size.height); 
    } 
    [self addSubview: label_to_add]; 

    return self; 
} 

Si todavía tiene un bucle infinito, una pausa en el depurador y buscar el patrón método recurrente en el seguimiento de la pila. Este patrón te dará el lugar donde el código ingresa al ciclo infinito.

+0

Thansk esto era parte del problema, la otra parte era que lo tenía en loadView not viewDidLoad, pero esto ayudó muchísimo, muchas gracias – AgentRegEdit

Cuestiones relacionadas