【问题标题】:Button Won’t Change Image On-Click After User Goes to Home Screen用户进入主屏幕后,按钮不会在单击时更改图像
【发布时间】:2015-12-01 19:26:18
【问题描述】:

我有 5 个不同的按钮,我希望用户能够在其中选择一个(如果未选中),如果已选中则取消选择,同时取消选择另一个按钮(如果已选中) .

正常 = 未选中时的按钮图像 填充 = 选中时的按钮图像

为此,我创建了一个 if/else 语句:

@IBAction func option1ButtonAction(sender: AnyObject) {

if (option1Button.currentImage == UIImage(named: "normal")) {

    if option2Button.currentImage == UIImage(named: "filled") || option3Button.currentImage == UIImage(named: "filled") || option4Button.currentImage == UIImage(named: "filled") || option5Button.currentImage == UIImage(named: "filled") {

        option1Button.setImage(filled, forState: .Normal)

        option2Button.setImage(normal, forState: .Normal)

        option3Button.setImage(normal, forState: .Normal)

        option4Button.setImage(normal, forState: .Normal)

        option5Button.setImage(normal, forState: .Normal)

    }

    else {

        option1Button.setImage(filled, forState: .Normal)

    }

}

else {

    option1Button.setImage(normal, forState: .Normal)

}

} 之后我做了一个 if option1Button.currentImage == UIImage(named: "filled") 语句。一切都按照我想要的方式工作,但是我有一个问题。每当用户按下主页按钮然后直接返回应用程序,按钮仍然具有“正常”图像,即使单击时也是如此。

在 viewDidDisappear 函数中,我输入了以下代码,希望能解决这个问题:

option1Button.setImage(normal, forState: .Normal)

option2Button.setImage(normal, forState: .Normal)

option3Button.setImage(normal, forState: .Normal)

option4Button.setImage(normal, forState: .Normal)

option5Button.setImage(normal, forState: .Normal)

但我仍然遇到问题。如果提供任何帮助,我将不胜感激。

【问题讨论】:

    标签: ios image swift button xcode6


    【解决方案1】:

    单击时您需要从按钮更改图像!我明白了吧?

    对于您的问题,我相信您可以做不同的事情。例如我做一个代码。

    首先设置按钮状态的图像。

    option1Button.setImage(normal, forState: UIControlState.Normal);
    option1Button.setImage(filled, forState: UIControlState.Disabled);
    
    option2Button.setImage(normal, forState:  UIControlState.Normal);
    option2Button.setImage(filled, forState: UIControlState.Disabled);
    

    第二次我做了一个函数,这个函数改变你按钮的状态。

    selectButton(button:UIButton)
    {
        option1Button.enabled = button != option1Button;
        option2Button.enabled = button != option2Button;
    }
    

    现在您只需以这种方式实现您的操作,其他细节是您不需要为每个按钮创建更多功能。 @IBAction func option1ButtonAction(sender: AnyObject) 只创建一个动作 @IBAction func onButtonClicked(sender: AnyObject) 和这里的所有按钮一样...记住只在 selectButton(button:UIButton) 函数中增加你的按钮,或者如果更喜欢你可以创建一个集合并在那里按下你的按钮并在 @987654326 中做一会儿@

    @IBAction func onButtonClicked(sender: AnyObject) {
    
        if let clickedButton = sender as? UIButton{
            selectButton(clickedButton);
        }
    
    }
    

    如果我正确理解了您的问题,希望对您有所帮助!

    【讨论】:

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