2010-06-07 19 views
5

Creo una vista personalizzata nel tocco di cacao che viene sovraclassata da UIView e nel mio controller principale la inizializzo e quindi la aggiungo come vista secondaria alla vista principale, ma quando la aggiungo a la vista principale chiama di nuovo il mio metodo di inizializzazione e causa un ciclo infinito. Sto andando a creare la mia vista personalizzata in modo errato? Ecco la MainViewCocoa Touch: creazione e aggiunta di una vista personalizzata

- (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]; 
} 

e 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; 
} 

risposta

1

Perché hai messo la dichiarazione [super init] al termine della inizializzazione? Quando si sottoclassa, di solito si mette questa affermazione all'inizio del metodo.

Per le sottoclassi UIView, l'inizializzatore designato durante la creazione di viste nel codice è initWithFrame:, quindi è necessario chiamarlo prima di aggiungere l'etichetta e l'immagine. È possibile utilizzare l'immagine per calcolare il frame necessario per la visualizzazione personalizzata.

-(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; 
} 

Se avete ancora un loop infinito, mettere in pausa il debugger e cercare il modello di metodo di ricorrente nella traccia dello stack. Questo modello ti darà dove il codice entra nel ciclo infinito.

+0

Thansk questo era parte del problema, l'altra parte era che l'ho avuto in loadView not viewDidLoad, ma questo ha aiutato incredibilmente grazie mille – AgentRegEdit

Problemi correlati