UIButton状态改变

时间:2011-08-04 06:19:06

标签: iphone objective-c 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]

1 个答案:

答案 0 :(得分: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];
    }

有效。