Ios 用户进入主屏幕后,按钮不会在单击时更改图像

Ios 用户进入主屏幕后,按钮不会在单击时更改图像,ios,image,swift,button,xcode6,Ios,Image,Swift,Button,Xcode6,我有5个不同的按钮,我希望用户能够选择一个,如果它没有被选中,并取消选择,如果它已经被选中,同时取消选择另一个按钮,如果它被选中 正常=未选中时的按钮图像填充=选中时的按钮图像 为此,我创建了一个if/else语句: @iAction func Option1ButtoActionSender:任何对象{ if (option1Button.currentImage == UIImage(named: "normal")) { if option2Button.currentImage

我有5个不同的按钮,我希望用户能够选择一个,如果它没有被选中,并取消选择,如果它已经被选中,同时取消选择另一个按钮,如果它被选中

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

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

@iAction func Option1ButtoActionSender:任何对象{

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==uiImageName:filled”语句。每件事都按照我想要的方式进行,但是我有一个问题。每当用户按下home按钮,然后右键返回应用程序时,即使单击按钮,按钮仍具有“正常”图像

在ViewDidEnglishe函数中,我添加了以下代码,希望能够解决此问题:

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)

但我仍然有问题发生。如果您能提供任何帮助,我将不胜感激。

您需要在单击按钮时更改图像!我明白,对吗

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

第一个设置按钮中状态的图像

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 Option 1ButtonactionSender:AnyObject仅创建一个操作@IBAction func onButtonClickedsender:AnyObject和这里的所有按钮一样。。。请记住,只能在selectButton按钮:UIButton功能中增加您的按钮,或者如果您更愿意,您可以创建一个集合并按下按钮,然后在selectButton中执行一段时间


如果我正确理解了你的问题,我希望能帮助你

单击时,您需要更改按钮中的图像!我明白,对吗

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

第一个设置按钮中状态的图像

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 Option 1ButtonactionSender:AnyObject仅创建一个操作@IBAction func onButtonClickedsender:AnyObject和这里的所有按钮一样。。。请记住,只能在selectButton按钮:UIButton功能中增加您的按钮,或者如果您更愿意,您可以创建一个集合并按下按钮,然后在selectButton中执行一段时间

如果我正确理解了你的问题,我希望能帮助你