为什么这段代码中没有出现view2? (将 UIView 2 合并到 UIView 1 中)
为什么这段代码中没有出现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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
view2
实际上并未将标签添加到自身。你错过了这个:换句话说,尝试:
view2
isn't actually adding the label to itself. You're missing this:In other words, try:
在您的测试视图控制器行中...
...添加...
之后view2 现在显示吗?请注意,将两个视图的 alpha 设置为 0.5,以确保一个视图不会遮挡另一个视图。
In your test view controller line after...
...add...
Does view2 show now? Alertnately, set the alpha of both views to 0.5 to make sure one is not obscuring the other.