简体   繁体   English

swift - 按下后终止的自定义按钮

[英]swift - customised button that terminate when pressed

I am trying to create a customised UIButton. 我正在尝试创建一个自定义的UIButton。 This customised button shows an unchecked box which will turn into a checked box, when the user presses the button. 此自定义按钮显示未选中的框,当用户按下按钮时,该框将变为复选框。 It will then return back to unchecked when user presses the checked box. 当用户按下复选框时,它将返回到未选中状态。

I have a runtime error. 我有一个运行时错误。 Debugger shows that it terminates because of an uncaught exception of type NSException. 调试器显示它因NSException类型的未捕获异常而终止。

Where does it goes wrong? 哪里出错了?

Code for the checked box 选中框的代码

import UIKit

class CheckBox: UIButton {

    //Images
    let checkedImage = UIImage(named: "checked")
    let unCheckedImage = UIImage(named: "unchecked")

    //bool property
    var isChecked:Bool = false {
        didSet {
            if isChecked == true{
                self.setImage (checkedImage, forState: .Normal)
            }
            else {
                self.setImage(unCheckedImage, forState: .Normal)
            }
        }
    }

    override func awakeFromNib() {
        self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        self.isChecked = false
    }

    func buttonClicked(sender:UIButton) {
        if(sender == self) {
            if isChecked == true {
                isChecked = false
            } else {
                isChecked = true
            }
        }
    }
}

If you want to use selected attribute then you can do the following: 如果要使用所选属性,则可以执行以下操作:

class CheckBox: UIButton {

//Images
let checkedImage = UIImage(named: "checked")
let unCheckedImage = UIImage(named: "unchecked")

    //bool property
    override func awakeFromNib() {
        self.addTarget(self, action: "buttonClicked", forControlEvents: UIControlEvents.TouchUpInside)

        self.setImage (checkedImage, forState: .Selected)

        self.setImage(unCheckedImage, forState: .Normal)
    }

    func buttonClicked(sender:UIButton) {
        if(sender == self) {

            self.selected = !self.selected
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速按下主页按钮时暂停计时器 - Pausing timers when home button is pressed swift 检测何时在 Swift 中的 IBAction 之外按下按钮? - Detect when a button is pressed outside of the IBAction in Swift? 快速按下按钮时如何翻转整个Tableview? - How can I flip an entire Tableview when a button is pressed in swift? 按下按钮并有提示时打印输出-Xcode Swift Apple Watch? - Print output when button is pressed and there is a segue - Xcode Swift Apple Watch? 在Swift中按下特定的键盘按钮 - Specific keyboard button pressed in Swift 通过按下Swift按钮创建多个UIView - Creating multiple UIViews from button pressed Swift 在Swift中按下按钮时使对象重置为固定(x,y)坐标 - Getting an object to reset to a fixed (x,y) coordinate when a button is pressed in Swift 迅捷的…如何在按下按钮并将其恢复为正常状态时更改图像? - Swift… how can I make an image change when a button its pressed and return it back to normal? iOS、Swift、UIAlertController、UIAlertAction - 如何在按下按钮时关闭关闭动画? - iOS, Swift, UIAlertController, UIAlertAction - How to turn dismiss animation off when button is pressed? 如何创建带有isHidden代码的按钮并在按下按钮时获取UIAlert? 然后可见-迅速4 - How can I create a button with isHidden code and get UIAlert when it be pressed? and be visible after that - swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM