【问题标题】:Swift UIButton .setTitle not workingSwift UIButton .setTitle 不起作用
【发布时间】:2015-12-22 14:56:16
【问题描述】:

我正在以编程方式在我的 VC 上创建一组按钮。我在创建按钮时为按钮添加了一个目标,在被调用的函数中,我正在更改背景图像并尝试删除标题。但是,标题并没有改变,但背景图像正在改变,这对我来说没有意义。

func createButton(xPos: Int, yPos: Int, width: Int, height: Int, buttonIndex: Int) -> UIButton {
    var button = UIButton()
    button.frame = CGRect(x: xPos , y: yPos, width: width, height: height)
    button.userInteractionEnabled = true
    button.layer.masksToBounds = true
    var buttonImage = UIImage(named: "Display_Icon")
    button.tag = buttonIndex
    button.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
    button.setTitle("\(buttonIndex)", forState: UIControlState.Normal)
    var titleColor = UIColor(red: 0.0, green: 104/255.0, blue: 178/255.0, alpha: 1.0)
    button.setTitleColor(titleColor, forState: UIControlState.Normal)
    button.addTarget(self, action: "screenSelected:", forControlEvents: UIControlEvents.TouchUpInside)
    return button
}

func screenSelected(button: UIButton!) {

    if hasError { hasError = false; return }
    let screenIndex = button.titleLabel!.text!

    if contains(buttonsSelectedArray, screenIndex) {
        return
    }

    buttonsSelectedArray.append(screenIndex)
    var buttonImage = UIImage(named: "DisplaySelected_Icon")
    button.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
    button.setTitle("", forState: UIControlState.Normal)
    var titleColor = UIColor.clearColor()
    button.setTitleColor(titleColor, forState: UIControlState.Normal)

【问题讨论】:

  • 使用调试器查看您的代码是否正在执行。

标签: swift cocoa-touch uibutton


【解决方案1】:

试试这个,

在旧方法(Obj-c)中添加目标按钮的问题

func createButton(xPos: Int, yPos: Int, width: Int, height: Int, buttonIndex: Int) -> UIButton {
    ....
    ....
button.addTarget(self, action: #selector(UIViewController. screenSelected(_:)), for: UIControlEvents.touchUpInside)
    return button
}  





@IBAction func screenSelected(button: UIButton!) {

    if hasError { hasError = false; return }
    let screenIndex = button.titleLabel!.text!

    if contains(buttonsSelectedArray, screenIndex) {
        return
    }

    buttonsSelectedArray.append(screenIndex)
    var buttonImage = UIImage(named: "DisplaySelected_Icon")
    button.setBackgroundImage(buttonImage, forState: .normal)
    button.setTitle("", forState: .normal)
    var titleColor = UIColor.clearColor()
    button.setTitleColor(titleColor, forState: .normal)
}

【讨论】:

  • @sasquatch 我的答案已更新为 swift 3,请检查。如果您有任何疑问,请随时发表评论,谢谢您
  • 我只是不明白它是如何回答这个问题的。 OP 已经在使用setTitle 但没有得到想要的结果
  • @sasquatch,抱歉给您带来不便,请在上面尝试,我已针对您的问题进行了更新:)
【解决方案2】:

你需要像这样设置你的按钮标题

button.setTitle("Button Title Here", for: UIControlState.normal)

IOS不再支持使用

button.titleLabel?.text= "按钮标题"

因为您需要使用其 UIControlState 来设置您的标题,它可以是选中的、正常的、突出显示的或其他。

请参阅此https://developer.apple.com/reference/uikit/uibutton/1624018-settitle?language=objc 中的文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2021-12-14
    • 2011-11-30
    相关资源
    最近更新 更多