2011-08-04 112 views
0

我已經使用代碼創建的按鈕如下所示 -的UIButton狀態變化

UIImage *kalenderImage = [UIImage imageNamed:@"start_icon_calendar_u.png"]; 
UIImageView *kalenderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease]; 
[kalenderImageView setImage:kalenderImage]; 
UILabel* kalendarLabel = [[[UILabel alloc] initWithFrame:CGRectMake (0, kalenderImage.size.height/2-15, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease]; 
kalendarLabel.text = @"Kalender"; 
[kalenderButton addSubview:kalenderImageView]; 
[kalenderButton addSubview:kalendarLabel]; 
[kalenderButton addTarget:self action:@selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside]; 

我需要改變按鈕的圖像爲UIControlStateHighlighted狀態。我怎樣才能做到這一點 ? 我不想使用

KalenderButton setBackgroundImage:@"" forState:] 
[KalenderButton setImage:@"" forState] 

回答

0

它很簡單。看下面的代碼。它檢查文件被鎖定並相應地更改按鈕的圖像。您可以根據您的需要編輯代碼。

if (fileLocked) { 
     UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"icon-lock.png"]]; 
     [lockButton setImage:image forState:UIControlStateNormal]; 
    } else { 
     UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"icon-unlock.png"]]; 
     [lockButton setImage:image forState:UIControlStateNormal]; 
    } 

它的工作原理。