【问题标题】:Changing UIButton background image on event在事件中更改 UIButton 背景图像
【发布时间】:2013-02-24 12:00:34
【问题描述】:

UISwitch 从“OFF”变为“ON”时,我希望UIButton 的背景图像发生变化。我有以下代码:

- (IBAction)switchValueChanged {
    if (Hard1ON.on) {
        UIImage *buttonImage = [UIImage imageNamed:@"red.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:Hard1];
        NSLog(@"Change");
    }
    else {
        UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:Hard1];
    }
}

当我按下开关时,控制台会正确记录,所以我知道它正在工作,但是按钮的背景没有改变。

作为参考,Hard1ON 是我的UISwitchHard1 是我的UIButton

【问题讨论】:

  • 我为你添加了一个编辑。希望对您有所帮助。
  • @flexaddicted 非常感谢,还有一个问题,如何将此按钮移动到屏幕上的其他位置?
  • 您需要使用[self.hardButton setFrame:CGRectMake(0, 0, 200, 50)]; 其中CGRectMake(x, y, width, height);。希望有帮助。另请参阅我对此的回答:stackoverflow.com/questions/5361369/…
  • 不客气。干杯。

标签: ios objective-c uibutton uiswitch


【解决方案1】:

一次添加你的按钮,最常见的方法是在viewDidLoad中添加它,然后在执行切换时更改它的图像。

所以,在viewDidLoad 方法中

// create the button
[self.view addSubview:Hard1];

那么,在你的值改变方法中

if (Hard1ON.on) {
    UIImage *buttonImage = [UIImage imageNamed:@"red.jpg"];
    [Hard1 setImage:buttonImage forState:UIControlStateNormal];
    NSLog(@"Change");
} else {
    UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
    [Hard1 setImage:buttonImage forState:UIControlStateNormal];
}

注意 使用驼峰命名法命名变量

例如,Hard1 应该看起来像 hard1。最好将其命名为hard1Button 或类似名称。这同样适用于开关元件。例如,hard1OnSwicth

编辑

我会尝试在viewDidLoad 方法中以编程方式创建按钮。

- (void)viewDidLoad
{ 
    [super viewDidLoad];

    self.hardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.hardButton setFrame:CGRectMake(0, 0, 200, 50)]; // set the x,y,width and height based on your specs
    UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
    [self.hardButton setImage:buttonImage forState:UIControlStateNormal];
    [self.view addSubview:self.hardButton];
}

hardButton 是对在 .h 中声明的按钮的引用(例如)

@property (nonatomic, assign) UIButton* hardButton;

像这样合成它(如果你愿意,如果不是,Xcode 会照顾你)

@synthesize hardButton = _hardButton;

如果您使用 strongretain 而不是 assign 并且您不使用 dealloc 中的 ARC 版本,则喜欢

- (void)dealloc
{ 
   [_hardButton release];
   [super dealloc];
}

现在,你的开关值改变了。

- (IBAction)switchValueChanged
{
    UIImage* buttonImage = nil;
    if (Hard1ON.on) {
        buttonImage = [UIImage imageNamed:@"red.jpg"];
    } else {
        buttonImage = [UIImage imageNamed:@"white.jpg"];
    }

    [self.hardButton setImage:buttonImage forState:UIControlStateNormal];
}

【讨论】:

  • 感谢您的帮助,但是当我在进行更改后尝试运行程序时,出现this。关于camelCase表示法-我通常这样做,但是hard1的外观让我不高兴哈哈。例如,hardOne 在我看来还不错。我只是在这个程序的非常基本的阶段,所以我会开始清理它,但我想最好从一开始就保持整洁。再次感谢您的建议。
  • 请详细说明您的问题。不要提供外部链接。
  • StackOverflow 有一个内置的图像共享空间。
  • 对不起,我没有意识到。它以绿色突出显示我的 AppDelegate 的一部分,并显示“线程 1:信号 SIGABRT”。另外 - 我的问题的哪一部分你想要更详细的@flexaddicted?
  • @LouisHolley 我的意思是,如果可能的话,您应该上传问题中的图片。这样,其他人可以看到问题并提出修复建议。非常感谢。
【解决方案2】:

我想你想使用-setBackgroundImage:forState: 而不是-setImage:forState:

而且,正如@flexaddicted 所说,您只想添加一次按钮。

【讨论】:

  • 感谢@iain 的回复,但按钮仍然没有影响
【解决方案3】:

如果您使用按钮作为开关,您可以喜欢这种方式。

  _btnMale = [UIButton buttonWithType:UIButtonTypeCustom];
   [_btnMale setBackgroundImage:[ UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
   [_btnMale setBackgroundImage:[ UIImage imageNamed:@"checkMark.png"] forState:UIControlStateSelected];
   _btnMale.tag = 1;
   _btnMale.frame = CGRectMake(150 , 9, 25, 25);
   [_btnMale addTarget:self action:@selector(selectGender:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:_btnMale];

根据需要在 viewDidLoad/viewDidAppear 方法中为选中状态和正常状态设置按钮图像。声明后,在您的目标方法中添加此代码。

-(void)selectGender:(UIButton*)sender{

  sender.selected = ! sender.selected;

}

然后将其他按钮设置为选中NO。然后按钮将像开关一样工作。

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多