2011-05-18 1151 views
8

Tengo un archivo xib con un pequeño UIView en él, que a su vez contiene algunas etiquetas. Ahora, estoy intentando cargar ese UIView en un UIViewController existente, y cambiar uno de los textos de las etiquetas. La razón por la que quiero hacerlo de esta manera es que el UIView se reutilizará con diferentes textos de etiquetas, así que pensé que hacer una clase personalizada y cargarlo desde un xib sería la mejor manera de hacerlo.Cargando UIView desde xib, se bloquea al intentar acceder a IBOutlet

He intentado varias formas de cargarlo y lo he mostrado con éxito en mi viewcontroller. El problema es que, una vez que trato de vincular realmente un IBOutlet en Interface Builder, y acceder a él, mi aplicación falla.

He creado una clase personalizada UIView, que se ve así:

CoverPanel.h

@interface CoverPanel : UIView { 

    IBOutlet UILabel *headline; 

} 

@property (nonatomic, retain) IBOutlet UILabel *headline; 

@end 

CoverPanel.m

@implementation CoverPanel 

@synthesize headline; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     // Initialization code. 
     // 
     self = [[[NSBundle mainBundle] loadNibNamed:@"CoverPanel" owner:self options:nil] objectAtIndex:0]; 
    } 
    return self; 
} 

En el CoverPanel.xib, me han vinculado el UILabel a la salida del título. En mi viewcontroller, así es como se crea la instancia CoverPanel, y aquí es donde se bloquea:

CoverPanel *panelView = [[CoverPanel alloc] initWithFrame:CGRectMake(0,0,300,100)]; 

Hasta ahora, todo bien. Muestra el UIView exactamente como se muestra en .xib. Pero tan pronto como trato de cambiar el headline.text así:

panelView.headline.text = @"Test"; 

se estrella con este error: Terminación de aplicación debido a excepción no detectada 'NSInvalidArgumentException', razón: '- [titular UIView]: Selector no reconocido enviado a instancia 0x5c22b00 '

Puede que sea algo muy pequeño que estoy pasando por alto, pero hasta ahora me ha estado volviendo loco por horas y horas. ¿Alguien tiene alguna idea?

Respuesta

20

Has reasignado la clase de tu vista personalizada a la vista que vivía en tu xib. No necesitas hacer eso porque entonces lo que obtuviste fue la vista desde el xib y acabas de filtrar tu CoverPanel. Por lo tanto, simplemente reemplace su inicializador:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     // No need to re-assign self here... owner:self is all you need to get 
     // your outlet wired up... 
     UIView* xibView = [[[NSBundle mainBundle] loadNibNamed:@"CoverPanel" owner:self options:nil] objectAtIndex:0]; 
     // now add the view to ourselves... 
     [xibView setFrame:[self bounds]]; 
     [self addSubview:xibView]; // we automatically retain this with -addSubview: 
    } 
    return self; 
} 
+1

Oh, por supuesto, tiene mucho sentido. Funciona ahora, gracias! – indivisueel

+0

¡¡¡Gran respuesta - !! – Woodstock

Cuestiones relacionadas