0

我正在尝试使用一个按钮在 UIImageView 对象中的两个图像之间切换。我一直在寻找与我的具体问题接近的问题/答案,但没有运气,所以这里......

MainViewController.h
 IBOutlet UIButton *selectionButton;
 UIImageView *selectionStatus;
 @property(nonatomic, retain) UIButton    *selectionButton;
 @property(nonatomic, retain) UIImageView *selectionStatus;
 - (IBAction)selectionButtonPressed: (id)sender;

MainViewController.m
 @synthesize selectionButton;
 @synthesize selectionStatus;
 - (IBAction)selectionButtonPressed: (id)sender
 {
   if (selectionStatus.highlighted == YES)  
      selectionStatus.highlighted = NO;
   else 
      selectionStatus.highlighted = YES;
 }

 -(void)viewWillAppear:(BOOL)animated {
   // selection images
   selectionStatus = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NotSelected.png"]  
                                       highlightedImage:[UIImage imageNamed:@"Selected.png"]];
 }  

当我在模拟器中运行它时,图像不会出现在“SelectionStatus”中,所以我想我错过了一些大事。帮助将不胜感激。谢谢!

我应该对按钮多说一些。selectionButton 是一个自定义按钮,它覆盖 selectionStatus 图像视图以及显示计算结果的标签。点击 selectionButton 选择标签中的值以包含在最多三个选定标签值的平均值计算中。selectionStatus 中显示的图像是一个复选标记,可根据选择状态更改颜色。我发现了另一种可行的方法,但似乎并不优雅:

- (IBAction)selectionButtonPressed: (id)sender
{
    if (toggleValue == YES) {
        toggleValue = NO;
        UIImage *unselect = [UIImage imageNamed:@"NotSelected.png"];
        selectionStatus.image = unselect;
    }
    else {
        toggleValue = YES;
        UIImage *select = [UIImage imageNamed:@"Selected.png"];
        selectionStatus.image = select;
    }
}

这种方式不需要使用viewWillAppear,初始化是在viewDidLoad中完成的。感谢您的意见和建议。

4

1 回答 1

0

在 ib 中,设置按钮的背景图像属性。我认为您的图像非常大,实际上当您将按钮的状态设置为突出显示时,它会显示在您的按钮上。试试看,我不确定。

于 2012-06-14T01:48:39.613 回答