2010-07-12 19 views
1

我創建了一個UIButton s的數組。我需要在不同的時間以編程方式更改這些按鈕上的圖像。暫時,爲了讓這個工作起作用,我想在按下按鈕時改變圖像,但這不是真正發生的時間。iphone uibutton:我想更改UIControlStateNormal圖片

所以,我創建按鈕的排列,並安排他們在屏幕上的網格:

for (UInt16 index=0; index < (mNumColumns * mNumRows); index++) 
{ 
    <....Snipped code for setting the x,y,width,height vars...> 
    UIButton *thisCell = [UIButton buttonWithType:UIButtonTypeCustom]; 
    thisCell.tag = index; 
    thisCell.frame = CGRectMake(currentX, currentY, cellWidth, cellHeight); 
    [thisCell addTarget:self action:@selector(buttonPushed:) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [thisCell setImage:[UIImage imageNamed:@"cellBackground.png"] 
     forState:UIControlStateNormal]; 
    [[self view] addSubview:thisCell]; 
} 

---然後按鈕handler--

-(void)buttonPushed:(UIView*)clickedButton 
{ 
    NSLog(@"Click %d", clickedButton.tag); 
    [clickedButton setImage:[UIImage imageNamed:@"cellEmpty.png"] 
     forState:UIControlStateNormal]; 
    [clickedButton setNeedsDisplay]; 
} 

1)按鈕的初始網格顯示正確。
2)如果我將初始網格的PNG文件名更改爲cellEmpty.png,它將與新圖像一起工作,所以我知道圖像可以顯示。 3)我在調試器中看到「Click n」,所以正確的按鈕調用buttonPushed

但是,正如你所猜測的那樣,圖像沒有被更新。我不認爲我現在想要處理按鈕狀態....因爲圖像變化並不是真正需要在現實生活中進行推演的按鈕(這種變化將是通過網絡下載新圖像的結果)。

+0

在你的buttonPressed:方法中,你有clickedButton作爲UIView,因此setImage:forState:什麼也不做。嘗試鍵入它到一個按鈕,看看會發生什麼 – 2010-07-12 17:12:24

+0

你的意思是做這個?
- (void)buttonPushed :(UIButton *)clickedButton

沒有幫助。雖然我認爲它不應該有所作爲,因爲傳遞的對象仍然是一個UIButton,不管類型轉換是否正確?我是Objective C的新手,但我認爲你可以作爲超類傳遞,並且仍然可以獲得派生的消息處理? – Brett 2010-07-12 18:44:52

回答

0

感謝您的幫助。這裏還有一些有趣的事情我需要弄清楚。 創建按鈕的視圖是標籤欄上的一個框架,如果我將標籤欄更改爲另一個視圖並返回,圖像已更新。

我會在解決問題時更新答案。