0

このコードにview2が表示されないのはなぜですか?結果として、ローカルのView1ラベルが表示され、上部に赤い境界線があり、全体的に緑の境界線内に表示されますが、view2は何も表示されません。つまり、「View2LabelText」というテキストのラベルは表示されません。

test11ViewController.m

@implementation test11ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    View1 *view1 = [[[View1 alloc] initWithFrame:CGRectMake(0.0, 0.0, 400, 100) ] autorelease];
    view1.layer.borderColor = [UIColor redColor].CGColor;
    view1.layer.borderWidth = 1;
    [self.view addSubview:view1];
}
@end

View1.m

@implementation View1
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Local Label
        CGFloat width = self.frame.size.width;
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
        label.text = @"View1 Label Text";
        label.layer.borderColor = [UIColor greenColor].CGColor;
        label.layer.borderWidth = 1.0;
        [self addSubview:label];

        // External - Label2
        View2 *view2 = [[[View2 alloc] initWithFrame:CGRectMake(0.0, 30, width, 30)] autorelease];
        [super addSubview:view2];   
    }
    return self;
}
@end

View2.m

@implementation View2
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = self.frame.size.width;
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
        label.text = @"View2 Label Text";   // Does  NOT appear in output
        label.layer.borderColor = [UIColor blueColor].CGColor;
        label.layer.borderWidth = 1.0;
    }
    return self;
}
@end
4

2 に答える 2

3

view2実際にはラベルをそれ自体に追加していません。あなたはこれを見逃しています:

[self addSubview:label];

つまり、次のことを試してください。

@implementation View2
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = self.frame.size.width;
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
        label.text = @"View2 Label Text";   // Does  NOT appear in output
        label.layer.borderColor = [UIColor blueColor].CGColor;
        label.layer.borderWidth = 1.0;
        [self addSubview:label];  // NEW LINE HERE
    }
    return self;
}
@end
于 2011-03-24T12:24:29.057 に答える
0

後のテストビューコントローラー行で...

[self.view addSubview:view1];

...追加...

[self.view sendSubviewToBack:view1];

view2 は表示されますか? 注意して、両方のビューのアルファを 0.5 に設定して、一方が他方を覆い隠さないようにします。

于 2011-03-24T12:03:01.593 に答える