我有一個UIView,它有幾個按鈕作爲子視圖添加到它。目前我在drawRect中有按鈕,我聽說這是一個壞主意,因爲可以多次調用drawRect。我試圖將這些UIButtons移動到initWithFrame,但它們不會被繪製。我應該如何解決這個問題?UIView drawRect vs initWithFrame
我沒有使用接口生成器,所以沒有NIB文件存在。 initWithFrame被調用。我通過添加一個NSLog(...)來檢查,並且我的NSMutableArrays正在被正確初始化。
我的initWitFrame:代碼目前很少。這是
- (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;
}
我試圖在其中添加類似
#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,那麼你可以分享你的initWithFrame:方法的代碼?是initWithFrame:叫? – tonklon 2010-07-30 21:24:26
添加了代碼。我不認爲這是有用的,雖然 – 2010-07-31 00:16:38
此代碼不顯示任何按鈕被創建或添加到視圖。您可以將代碼發佈到您嘗試將它們添加到-initWithFrame中的視圖中嗎? – 2010-07-31 13:12:59