2010-07-29 5 views
2

J'ai un UIView qui a plusieurs boutons ajoutés comme sous-vues. Actuellement, j'ai les boutons dans drawRect :, ce que j'ai entendu est une mauvaise idée parce que drawRect peut être appelé plusieurs fois. J'ai essayé de déplacer ces UIButtons vers initWithFrame, mais ils ne sont pas dessinés. Comment devrais-je résoudre ce problème?UIView drawRect vs initWithFrame

Je n'utilise pas de programme de création d'interface. Par conséquent, aucun fichier NIB n'est présent. Le initWithFrame est appelé. J'ai vérifié en ajoutant un NSLog (...), et mes NSMutableArrays sont correctement initialisés.

Mon initWitFrame: code a actuellement très peu. Il est

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) 
    { 
     results = [[NSMutableArray alloc] init]; 
     inputs = [[NSMutableArray alloc] init]; 
     format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"EEE, MMM dd, yyyy"]; 
     renc =[[RenameController alloc] init]; 
    } 
    return self; 
} 

J'ai essayé d'ajouter un Textlabel comme le

suivant
#define BOX_WIDTH 155.0 
#define BOX_OFFSET 45.00 
#define ROW_HEIGHT 27 

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) 
    { 
     results = [[NSMutableArray alloc] init]; 
     inputs = [[NSMutableArray alloc] init]; 
     format = [[NSDateFormatter alloc] init]; 
     [format setDateFormat:@"EEE, MMM dd, yyyy"]; 
     renc =[[RenameController alloc] init]; 

     double row=0.0; 
     [self makelabel:@"Start" at:CGRectMake(0, row, BOX_OFFSET, ROW_HEIGHT)]; 
    } 
    return self; 
} 

-(void) makelabel:(NSString *) str at:(CGRect) rect 
{ 
    UILabel *title = [[UILabel alloc] initWithFrame:rect]; 
    title.text = str; 
    title.textAlignment = UITextAlignmentLeft; 
    title.textColor = [UIColor blackColor]; 
    title.backgroundColor = [UIColor clearColor]; 
    title.font = [UIFont boldSystemFontOfSize:FONT_SIZE]; 
    [self addSubview:title]; 
    [title release]; 
} 
+1

OK, alors pouvez-vous votre part initWithFrame: Code? Est-ce que initWithFrame: est appelé? – tonklon

+0

Ajout du code. Je ne pense pas que ce soit si utile –

+0

Ce code n'affiche aucun bouton créé ou ajouté à la vue. Pourriez-vous publier le code dans lequel vous avez tenté de les ajouter à la vue dans -initWithFrame :? –

Répondre

7

@tonklon est sur place. De plus, le schéma habituel est d'avoir votre code d'initialisation personnalisé dans une troisième méthode, comme -sharedInit, qui est appelée après la super initialiseur appropriée:

- (id)sharedInit { 
    // do whatever, possibly releasing self on error and returning nil 
    return self; 
} 

- (id)initWithFrame: (CGRect)frame { 
    self = [super initWithFrame: frame]; 
    if (self) { 
     self = [self sharedInit]; 
    } 
    return self; 
} 

- (id)initWithCoder: (NSCoder *)aDecoder { 
    self = [super initWithDecoder: aDecoder]; 
    if (self) { 
     self = [self sharedInit]; 
    } 
    return self; 
} 
+0

+1 C'est mieux de cause. – tonklon

2

Je parie que la vue est chargé à partir d'un fichier nib? Il suffit d'utiliser le même code pour ajouter des sous-vues dans initWithCoder:

UIView Référence de la classe:

Si vous utilisez Interface Builder pour concevoir votre interface, cette méthode n'est pas appelée lorsque vos objets de vue sont suite chargé à partir du fichier nib. objets dans un fichier NIB sont reconstitue et initialisés en utilisant leur initWithCoder: méthode, qui modifie les attributs de la vue de faire correspondre les attributs stockés dans le fichier nib. Pour plus d'informations sur la façon dont les vues sont chargées à partir d'un fichier nib , voir le guide de programmation des ressources.

+1

Désolé, pas de constructeur d'interface dans cette maison! –

Questions connexes