ios - [UI按钮设置文本:]: unrecognized selector sent to instance

标签 ios swift

我已经阅读了有关此问题的几个答案,但似乎找不到适用于我的代码的答案。当我分配“myLabel”的“text”成员(如下)时,我得到:“[UIButton setText:]:无法识别的选择器发送到实例......”程序编译正常,但在遇到赋值语句时崩溃。

但是我可以分配甚至打印出同一对象的背景颜色。关于我做错了什么有什么想法吗?

//******** 导入 UIKit

class ViewController: UIViewController {

//*********  Button 1 *********//


@IBOutlet weak var lbl1: UILabel!


@IBAction func btn1(sender: AnyObject) {

    nextColorFor1++
    makeColor (self.lbl1, nextColor: nextColor1)

}

//****  Button 2  **********//


@IBOutlet weak var lbl2: UILabel!

@IBAction func btn2(sender: AnyObject) {

    nextColorFor2++
    makeColor (self.lbl2, nextColor: nextColorFor2)

}

//********************************//

func makeColor (myLabel:UILabel, nextColor:Int)
{
    switch nextColor
    {
    case 0: myLabel.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)
    case 1: myLabel.backgroundColor = UIColor(red: 0.2, green: 0, blue: 0, alpha: 0.2)
    case 2: myLabel.backgroundColor = UIColor(red: 0.3, green: 0, blue: 0, alpha: 0.3)
    case 3: myLabel.backgroundColor = UIColor(red: 0.4, green: 0, blue: 0, alpha: 0.4)
    case 4: myLabel.backgroundColor = UIColor(red: 0.5, green: 0, blue: 0, alpha: 0.5)
    default: print("end of colors")
    }

    print(myLabel.backgroundColor)
    myLabel.text = "4"  <<< --- This is where the crask occurs
}


override func viewDidLoad() {


    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

}

最佳答案

您似乎已将 UIButton(位于 Storyboard或 xib 上)作为代码中的 UILabel 连接,并且您试图将其视为标签。确保更换 IB 上的标签按钮,它就会起作用。

关于ios - [UI按钮设置文本:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669278/

相关文章:

ios - 如何为不同的屏幕尺寸设置不同的自动布局约束

ios - 将 CollectionView 或 ViewController 添加到 AlertView

swift - 为什么快速转换适用于浮点除法?

objective-c - 更改 xib 文件的背景颜色?

ios - 如何在 ios swift 的整个应用程序中打开音频播放器 View ?

ios - 将自定义 tableviewcell 连接到 Uitableview

swift - 为什么 Nil 与 swift 4 中预期的参数类型 'UnsafePointer<CGAffineTransform>' 不兼容?

ios - Swift 2.0 OneSignal .pushNotification

ios7 UIStatusBar 在 UIViewController 下

ios - 如何从应用内购买获取购买信息