按钮不会更改用户进入主屏幕后的点击图像
问题描述:
我有5个不同的按钮,我希望用户能够选择一个,如果它没有被选中,并取消选择它,如果它已被选中,同时如果选择了另一个按钮则取消选择。按钮不会更改用户进入主屏幕后的点击图像
正常=按钮的图像时选择
时,要做到这一点,我创建没有选择充满=按钮的图像的if/else语句:
@IBAction FUNC option1ButtonAction(发件人: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 if1Button.currentImage == UIImage(named:“filled”)语句,一切正常,但我有一个问题,当用户按下h ome按钮,然后直接进入应用程序,即使点击按钮,按钮仍然具有“正常”图像。
里面的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)
但我仍然有问题发生。我会很感激提供的任何帮助。
答
单击时需要更改按钮上的图像!我明白吗?
对于你的问题,我相信你可以做不同的事情。我举例说明一个代码。
首先设置按钮状态的图像。
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)
功能,或者如果preferrer您可以创建一个集合,并有把你的按钮,并做selectButton
@IBAction func onButtonClicked(sender: AnyObject) {
if let clickedButton = sender as? UIButton{
selectButton(clickedButton);
}
}
一会儿如果我正确理解你的问题,我希望帮助你!