UIView drawRect vs initWithFrame

J'ai une UIView qui dispose de plusieurs boutons ajoutés comme des sous-vues. Actuellement, j'ai des boutons dans le drawRect:, que j'ai entendu, c'est une mauvaise idée parce que drawRect peut être appelée plusieurs fois. J'ai essayé de déplacer ces UIButtons à initWithFrame, mais ils n'ont pas tiré. Comment dois-je résoudre ce problème?

Je ne suis pas en utilisant l'interface builder, donc il n'y a pas de PLUME fichiers présents. Le initWithFrame est appelé. J'ai vérifié par l'ajout d'un NSLog(...), et mon NSMutableArrays sont correctement initialisés.

Mon initWitFrame: code dispose actuellement de 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 suit

#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];
}
  • OK, alors peut votre part votre initWithFrame: Code? Est initWithFrame: appelé?
  • Ajouté le code. Je ne pense pas que c'est utile
  • Ce code ne pas afficher les boutons en cours de création ou ajouté à la vue. Pourriez-vous poster le code où vous avez tenté de les ajouter à la vue en initWithFrame:?
  • Pour ajouter encore plus de code.
InformationsquelleAutor John Smith | 2010-07-29