ios - 访问按钮标题给出了错误的值

标签 ios swift uibutton

在我的 Storyboard中,我的按钮属性是这样的:

enter image description here

您会在标题下看到值“3”。

我尝试通过以下方式在 IBAction 中以编程方式访问该值:

IBAction func bugTypeSelected(sender: UIButton) {
     print("title = \(sender.titleLabel?.text)")
}

打印输出的值是 title =Optional("Button") ,为什么它不是 Option("3")

最佳答案

尝试改用

if let title = sender.title(for: .normal) {
   print("title = \(title)")
}

关于ios - 访问按钮标题给出了错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40821311/

相关文章:

ios - 如何快速获取 iCloud 图片 URL

ios - 使用 NSPredicate 过滤对象和键(需要拆分)

android - 不使用应用程序 Radbeacon 检测信标?

ios - 线程 0 崩溃,SIGABRT 异常和 __pthread_kill

ios - 在标签栏 Controller 中嵌入 'show' segues

ios - UIButton执行三个 Action ?

ios 9 NData 即使调用 https url 也返回 nil

swift - 如何延迟迭代

iPhone UIButton 的作用类似于 UISwitch

ios - 突出显示要按下的按钮,并取消突出显示未按下的按钮SWIFT