Jamie Gordon
Jamie Gordon

Reputation: 61

Can't hide created UIButton

I have three UIButtons which display in a random order using:

NSMutableArray *indexArray = [NSMutableArray arrayWithObjects:
                              [NSValue valueWithCGRect:CGRectMake(20, 187, 280, 44)],
                              [NSValue valueWithCGRect:CGRectMake(20, 258, 280, 44)],
                              [NSValue valueWithCGRect:CGRectMake(20, 330, 280, 44)], nil];

//Randomize the array
NSUInteger count = [indexArray count];
for (NSUInteger i = 0; i < count; ++i) {
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [indexArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}

//Assign the frames
button1.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
button2.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue];
button3.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue];

For some reason I an unable to hide these buttons after they display a number of items. I have tried for example

button1.hidden = YES; and also [self.button1.hidden = YES];

Any ideas? Any help would be most appreciated.

Jamie

Upvotes: 0

Views: 134

Answers (3)

sergio
sergio

Reputation: 69027

To do this I use this:

 if ([questions count]== 11)
   { button1.hidden = YES; button2.hidden = YES; button3.hidden = YES } 

I would suggest you check two things:

  1. that you effectively take the branch;

  2. that your button* variables are not nil.

e.g.:

 if ([questions count]== 11)
 {
    NSLog(@"Enter branch with %x, %x, %x", button1, button2, button3);
    button1.hidden = YES; button2.hidden = YES; button3.hidden = YES;
 } 

(ignore the warning that you will get on NSLog).

Upvotes: 0

Lalit
Lalit

Reputation: 264

Pass tag to Buttons and use below code

 for (UIButton *btn in [self.view subviews]) 
    {
        if (btn.tag==1)
        {
            [btn removeFromSuperview];
        }
   }

and your problem will be resolved and revert me..

Upvotes: 1

Oh Seung Kwon
Oh Seung Kwon

Reputation: 431

These buttons are IBOUTLET?

You can think of another way to hide

like, '[UIView viewWithTag:(NSInteger)]

sample code is here

UIButton *tmpBtn = (UIButton *)[self.view viewWithTag:1]; // tag is your choice
tmpBtn.hidden = YES

Upvotes: 0

Related Questions