在 UIView 中添加 UIStuff

发布于 2024-09-29 14:21:09 字数 715 浏览 1 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(2

仅此而已 2024-10-06 14:21:09

在您列出的代码的第一部分中,您有:

[self.view insertSubview:carte1J1 atIndex:1];

在第二部分中,您有:

[viewJoueur1 addSubview:carte1J1];

因此,如果我理解正确的话,您已将 cartelJ1 添加到两个视图中。

然后,您隐藏这两个视图之一,但 self.view 仍然可见,并且它包含 cartelJ1,因此 cartelJ1 仍然可见。

如果我误解了您的代码,请纠正我......

In the first section of code that you listed, you have:

[self.view insertSubview:carte1J1 atIndex:1];

In the second section, you have:

[viewJoueur1 addSubview:carte1J1];

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...

音栖息无 2024-10-06 14:21:09

你是对的,那不是 记录行为:

隐藏带有子视图的视图有
隐藏这些子视图的效果和
他们可能拥有的任何视图后代。

您将 carte1J1 添加为 viewJoueur1 的子视图; viewJoueur1 是普通的 UIView 还是自定义子类?如果它是子类,您是否覆盖了 -setHidden:

如果它是标准 UIView,则此行为不会记录在案,您应该将其报告为错误

我注意到的一件事是,当您创建按钮时,将其添加为 self.view 的子视图,然后将其添加为 viewJoueur1 的子视图。视图一次只能是一个视图的子视图,因此第一个视图是多余的。

You’re right, that isn’t the documented behavior:

Hiding a view with subviews has the
effect of hiding those subviews and
any view descendants they might have.

You’re adding carte1J1 as a subview of viewJoueur1; is viewJoueur1 a plain UIView 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 of viewJoueur1. Views can only be a subview of one view at a time, so the first one is redundant.

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