2013-02-05 62 views
0

我有在使用隨機順序顯示下面有三個UIButtons:無法掩蓋創建的UIButton

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]; 

出於某種原因,我一個無法隱藏這些按鈕後,便顯示了一些項目。我試過了,例如

button1.hidden = YES;還有 [self.button1.hidden = YES];

任何想法?非常感激任何的幫助。

Jamie

+0

你應該顯示你究竟如何試圖隱藏他們...在哪裏,在哪個方法?什麼時候? – sergio

+0

我創建了一個小測驗應用程序。問題被加載到UILabel中,並從plist(通過NSMultableArray)加載到這些UIButtons中,當一定數量的問題被回答後,結果顯示出來,問題和按鈕應該消失。要做到這一點,我使用這個: 'if([questions count] == 11){button1.hidden = YES; button2.hidden = YES; button3.hidden = YES}'。我很困惑,爲什麼這些UIButtons不隱瞞。 –

回答

1

通行證標籤按鈕並使用下面的代碼

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

和你的問題將得到解決,並恢復我..

+0

這很好,非常感謝。 –

0

這些按鈕是IBOUTLET?

你能想到的另一種方式來隱藏

一樣,「[UIView的viewWithTag:(NSInteger的)

示例代碼是在這裏

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

要做到這一點,我用這個:

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

我建議你檢查兩件事情:

  1. 你有效地取用分支;

  2. 您的button*變量不是nil

例如爲:

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

(忽略你會得到的NSLog警告)。