ios - 自定义 UIButton 点击​​事件崩溃

标签 ios swift3 uibutton

UIButton 的子类

@objc class OpenCloseButton: UIButton {

    /* used NSNumber due to "-swift.h" collaboration file do not generate primitive types. 
    Hence, cannot use `Bool` */

    var isOpen: Bool = 0 
}

现在,我把 UIButton在 Storyboard 中并将其类别更改为 OpenCloseButton并将以下操作连接到该按钮:
@IBAction func openCloseDetails(_ button: OpenCloseButton) {

    if button.isOpen == false { // here it crashes

        button.isOpen = true
        self.closeDetails()

    } else {

        button.isOpen = false
        self.openDetails()
    }
}

当应用程序运行时,当我点击该按钮时它会崩溃
Threas: 1 EXC_BAD_ACCESS (code=2, address=0x06549gfr)
异常断点显示方法第二行的崩溃。

我在这里想念什么?

附加信息:

控制台在崩溃行有以下输出:
(lldb) po button▿ Optional<OpenCloseButton>(lldb) po button!<UIButton: 0x146059c0; frame = (728 12; 32 32); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x1469ca40>>

最佳答案

您能否在调用 if button.isOpen?.boolValue... 之前添加此内容? :

guard let button = button as? OpenCloseButton else {
    return
}

它只是为了测试目的。它是继续运行还是调用方法中的返回?

关于ios - 自定义 UIButton 点击​​事件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42292160/

相关文章:

iOS:如何在 iOS 中调整键盘覆盖区域的高度

iphone - IOS 中的 WebDAV 支持

IOS swift 如何在标签点击时在 TableView 中获取标签文本

iphone - UIButton 的图像丢失了新设置的图像

iphone - PushViewController 导航到下一个堆栈

javascript - 在移动 Chrome 上隐藏键盘不会改变窗口高度

ios - 在 iOS 中使用 google places api 找不到的地方

iOS11 - Realm 框架未构建

swift - Swift 3/iOS 10 中的漏洞

swift - swift 中的编程按钮不起作用