【问题标题】:Why isn't my UIButton Text changing when I call .setTitle? (Swift)为什么我调用 .setTitle 时我的 UIButton Text 没有改变? (迅速)
【发布时间】:2015-07-07 19:26:31
【问题描述】:

我有一个按钮可以在按下时更改另外两个按钮的文本。代码如下:

@IBAction func nextDay(sender: AnyObject) {
    selectedDate = selectedDate.set("day", value: selectedDate.day + 1)!
    //update button text
    monthDayYearButton.setTitle(formatter.stringFromDate(selectedDate), forState: .Normal)
    dayOfTheWeekButton.setTitle(selectedDate.weekdayName, forState: .Normal)
}

但是,只有 dayOfTheWeekButton 更改了它的标题。我知道这两个按钮之间的唯一区别是monthDayYearButton 使用属性文本而不是纯文本。我仅使用 Attributed Text 将字体设置为粗体。

我已经为每个状态按钮尝试过这个,包括 allZeros。

别担心我奇怪的 NSDate 操作,我安装了一个名为 SwiftDate 的可可豆荚。

【问题讨论】:

  • 你能验证 'monthDayYearButton' 不是 nil 吗?确保它在情节提要中正确连接。
  • 只需检查界面构建器中的按钮 IBOutlet 连接即可。

标签: ios swift uibutton


【解决方案1】:

尝试使用:
monthDayYearButton.setAttributedTitle(NSAttributedString, forState: UIControlState)

【讨论】:

  • 好的,谢谢! monthDayYearButton.setAttributedTitle(NSAttributedString(string: formatter.stringFromDate(selectedDate), attributes: [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]), forState: .Normal)
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 2018-09-24
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
相关资源
最近更新 更多