为什么这段代码中没有出现view2? (将 UIView 2 合并到 UIView 1 中)

发布于 2024-10-27 03:24:39 字数 1758 浏览 3 评论 0原文

为什么这段代码中没有出现view2?在结果中,我看到显示了本地 View1 标签,顶部有红色边框,并且在整体绿色边框内,但是我没有看到 view2 的任何内容?即带有文本“View2 Label Text”的标签不会出现。

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

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:(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-11-03 03:24:39

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

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
挽容 2024-11-03 03:24:39

在您的测试视图控制器行中...

[self.view addSubview:view1];

...添加...

[self.view sendSubviewToBack:view1];

之后view2 现在显示吗?请注意,将两个视图的 alpha 设置为 0.5,以确保一个视图不会遮挡另一个视图。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文