开发者

why doesn't view2 appear in this code? (incorporating UIView 2 within UIView 1)

开发者 https://www.devze.com 2023-02-19 16:37 出处:网络
Why doesn\'t view2 appear in this code?In the result I see the local View1 label shown, at the top with a red border, and within the overall green border, however I see nothing of view2? That is the l

Why doesn't view2 appear in this code? In the result I see the local View1 label shown, at the top with a red border, and within the overall green border, however I see nothing of view2? That is the label with text "View2 Label Text", does NOT appear.

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:(CGRe开发者_StackOverflowct)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


view2 isn't actually adding the label to itself. You're missing this:

[self addSubview:label];

In other words, try:

@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


In your test view controller line after...

[self.view addSubview:view1];

...add...

[self.view sendSubviewToBack:view1];

Does view2 show now? Alertnately, set the alpha of both views to 0.5 to make sure one is not obscuring the other.

0

精彩评论

暂无评论...
验证码 换一张
取 消