Cannot get this checkbox to build?

自闭症网瘾萝莉.ら 提交于 2019-12-13 07:57:46

问题


I tried to fix other errors so it may look tad different but this is the best i could do.

override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()
    Box49.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box49.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

    Box50.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box50.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

    Box51.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box51.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

}

@IBAction func Box49(_ sender: UIButton) {
    sender.isSelected = !(sender as AnyObject).isSelected
}


@IBAction func Box50(_ sender: UIButton) {
    sender.isSelected = !(sender as AnyObject).isSelected
}


@IBAction func Box51(_ sender: UIButton) {
    sender.isSelected = !(sender as AnyObject).isSelected
    }

回答1:


First of all you need to create 3 outlet of button because you are currently setting image to button action, So either set both selected and normal image in your interface builder or create 3 button outlet like below.

@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!

Now set the image to Button in either viewDidLoad or viewDidAppear not in didReceiveMemoryWarning also there is no need to cast sender to AnyObject to access isSelected property because sender type is UIButton and so you can directly access isSelected property.

var BoxOFF = #imageLiteral(resourceName: "UnCheckBox")
var BoxON = #imageLiteral(resourceName: "CheckBox") 
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
     box49.setImage(BoxOFF, for: .normal)
     box49.setImage(BoxON, for: .selected)

     box50.setImage(BoxOFF, for: .normal)
     box50.setImage(BoxON, for: .selected)

     box51.setImage(BoxOFF, for: .normal)
     box51.setImage(BoxON, for: .selected)
} 

And now set your button action this way.

@IBAction func Box49(_ sender: Button) {
     sender.isSelected = !sender.isSelected
}

@IBAction func Box50(_ sender: Button) {
     sender.isSelected = !sender.isSelected
}

@IBAction func Box51(_ sender: Button) {
     sender.isSelected = !sender.isSelected
}



回答2:


override func didReceiveMemoryWarning()
 {
    super.didReceiveMemoryWarning()
 }

The above method is called when there is low memory and you need to release any resources.

So you need to add the code from the above method into viewDidAppear or viewWillAppear.

So replace the code as below:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
     Box49.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box49.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

    Box50.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box50.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

    Box51.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
    Box51.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)

} 

One another improvement is replace your code

sender.isSelected = !(sender as AnyObject).isSelected

with

sender.isSelected = !sender.isSelected


来源:https://stackoverflow.com/questions/42628024/cannot-get-this-checkbox-to-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!