views:

39

answers:

2

Hi,

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

A: 

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.

Jeff Kelley
A: 

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

GregInYEG