在 UIView 中添加 UIStuff
我想在 UIView 中添加按钮,这样我就可以隐藏或不隐藏它们。
我的按钮的代码:
carte1J1=[UIButton buttonWithType:UIButtonTypeCustom];
carte1J1.tag=11;
carte1J1.frame=CGRectMake(60, 240, 50, 73.0);
[carte1J1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",imagecarte1] ] forState:UIControlStateNormal] ;
[carte1J1 addTarget:self action:@selector (clicCarte1J1) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:carte1J1 atIndex:1];
我的视图是 viewJoueur1,我尝试在视图中添加我的按钮,如下所示。
[viewJoueur1 addSubview:carte1J1];
为了测试我尝试隐藏 viewJoueur1:
viewJoueur1.hidden=YES;
但按钮仍然可见,我不明白为什么
I want to add my buttons in a UIView, like then I can hide or not them.
My button's code:
carte1J1=[UIButton buttonWithType:UIButtonTypeCustom];
carte1J1.tag=11;
carte1J1.frame=CGRectMake(60, 240, 50, 73.0);
[carte1J1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",imagecarte1] ] forState:UIControlStateNormal] ;
[carte1J1 addTarget:self action:@selector (clicCarte1J1) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:carte1J1 atIndex:1];
My view is viewJoueur1, I try to add my button in the view like this.
[viewJoueur1 addSubview:carte1J1];
And to test I try to hide viewJoueur1:
viewJoueur1.hidden=YES;
But the button is still visible and I don't understand why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您列出的代码的第一部分中,您有:
在第二部分中,您有:
因此,如果我理解正确的话,您已将 cartelJ1 添加到两个视图中。
然后,您隐藏这两个视图之一,但 self.view 仍然可见,并且它包含 cartelJ1,因此 cartelJ1 仍然可见。
如果我误解了您的代码,请纠正我......
In the first section of code that you listed, you have:
In the second section, you have:
So, you've added cartelJ1 to two views if I'm understanding correctly.
Then, you hide one of those two views, but self.view is still visible, and it contains cartelJ1, so cartelJ1 is still visible.
Please correct me if I'm misunderstanding your code...
你是对的,那不是 记录行为:
您将
carte1J1
添加为viewJoueur1
的子视图; viewJoueur1 是普通的 UIView 还是自定义子类?如果它是子类,您是否覆盖了-setHidden:
?如果它是标准 UIView,则此行为不会记录在案,您应该将其报告为错误。
我注意到的一件事是,当您创建按钮时,将其添加为 self.view 的子视图,然后将其添加为 viewJoueur1 的子视图。视图一次只能是一个视图的子视图,因此第一个视图是多余的。
You’re right, that isn’t the documented behavior:
You’re adding
carte1J1
as a subview ofviewJoueur1
; isviewJoueur1
a plainUIView
or a custom subclass? If it’s a subclass, have you overridden-setHidden:
?If it’s a standard UIView, then this behavior is not as documented and you should report it as a bug.
One thing that I notice is that when you create the button, you add it as a subview of
self.view
, then later add it as a subview ofviewJoueur1
. Views can only be a subview of one view at a time, so the first one is redundant.