2012-02-01 43 views
0

我在viewDidLoad方法intialised一個UIButton:的UIButton不顯示時使用隱藏= FALSE命令

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
resumeButtonTest = [UIButton buttonWithType:UIButtonTypeCustom]; 
    resumeButtonTest = [[UIButton alloc]initWithFrame:CGRectMake(500, 512, 48, 51)]; 
    resumeButtonTest.frame = CGRectMake(500, 512, 48, 51); 
    [resumeButtonTest setImage:[UIImage imageNamed:@"resumebutton_splash_passive.png"] forState:UIControlStateNormal]; 
    [resumeButtonTest setImage:[UIImage imageNamed:@"resumebutton_splash_ontouch.png"] forState:UIControlStateHighlighted]; 

refreshButtonTest = [UIButton buttonWithType:UIButtonTypeCustom]; 
refreshButtonTest = [[UIButton alloc]initWithFrame:CGRectMake(500, 600, 48, 51)]; 
refreshButtonTest.frame = CGRectMake(500, 600, 48, 51); 
[refreshButtonTest setImage:[UIImage imageNamed:@"refreshbutton_splash_passive.png"] forState:UIControlStateNormal]; 
[refreshButtonTest setImage:[UIImage imageNamed:@"refreshbutton_splash_ontouch.png"] forState:UIControlStateHighlighted]; 

[self.view addSubview:resumeButtonTest]; 
[self.view addSubview:refreshButtonTest]; 
refreshButtonTest.hidden = TRUE; 
resumeButtonTest.hidden =TRUE; 
} 

那我設置爲隱藏,直到下載失敗這樣的時刻,當它這樣做的方法得到所謂:

-(void)displayFailure{ 


refreshButtonTest.hidden = FALSE; 
resumeButtonTest.hidden = FALSE; 

}

並執行代碼時按鈕應該出現,但是沒有。 '隱藏'命令不起作用。我需要刷新視圖嗎?我真的不明白髮生了什麼事。這應該是非常簡單的,但它不是...

+1

搜索:所以把支票在像這樣d – 2012-02-01 11:33:56

回答

1

當您定義任何布爾變量時,通常會使用False和True。在UI屬性的Objective c中,我們總是使用YES或NO,而不是True或False。因此,使用YES和NO代替True或False

+1

難以置信。這爲我解決了它。 .hidden = FALSE失敗,但.hidden =沒有工作。 – Praxiteles 2014-08-31 06:30:19

+0

很高興:-) :-) .. – 2014-09-15 09:25:56

2

問題是我們將很多UIViews放在另一個上面,所以當我試圖隱藏和顯示按鈕時,隱藏了這些按鈕的視圖或所顯示的內容只是隱藏在另一個視圖背後....因此,請始終檢查您正在創建的視圖數量,只能在一個位置創建一次。

找到解決方案之前我們檢查過的其他可能的錯誤是與線程有關。如果你在視圖中隱藏了按鈕,並且你不在主線程中,那麼UI將不會更新,直到該線程已經連接到主線程。使用YES和NO的,而不是使用真與假,看看是否能工程

if ([NSThread isMainThread]) 
    NSLog(@"We are on main thread"); 
else 
    NSLog(@"Not on main thread, make sure you are if you want to do UI updates"); 
+0

非常感謝。你的最終提示解決了我的問題。我嘗試在一個線程中取消隱藏我的UIButton:'dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)'。它不能立即工作 - 只有6秒的延遲......現在我用'dispatch_async dispatch_get_main_queue()' - 這很好用! – DerWOK 2013-08-30 12:53:52