猛烈抨击我。应该这么简单。
在MyClass:
func configureButton(){
let btn: UIButton = UIButton(frame: CGRectMake(5, 75, 90, 20))
btn.backgroundColor = UIColor.greenColor()
btn.addTarget(self,
action: #selector(MyClass.buttonTapped),
forControlEvents: UIControlEvents.TouchUpInside)
addSubview(btn)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.titleLabel?.font = UIFont(name: "Helvetica", size: 40)
btn.titleLabel?.text = "tap me"
}
按钮显示绿色,按下时有效,但不显示文字。
答案 0 :(得分:5)
这一行是错的:
btn.titleLabel?.text = "tap me"
请勿尝试直接操作按钮的标题标签文字。始终通过按钮的官方标题设置器setTitle:forState:
。