【问题标题】:[UIButton setText:]: unrecognized selector sent to instance[UIButton setText:]:发送到实例的无法识别的选择器
【发布时间】:2016-02-27 11:43:38
【问题描述】:

我已阅读有关此问题的多个答案,但似乎找不到适用于我的代码的答案。当我分配“myLabel”的“text”成员(如下)时,我得到:“[UIButton setText:]: unrecognized selector sent to instance....”程序编译正常,但遇到赋值语句时崩溃。

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

//******** 导入 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.

}

}

【问题讨论】:

  • 还有一件事——按钮和标签都连在一起了。
  • 为避免混淆,请勿使用相同的名称,例如实例和局部变量 (myLabel)。
  • 按钮和标签连在一起是什么意思?
  • 有什么理由不赞成我的回答吗?我试过了,我放在这里。
  • 对不起各位。我认为当我试图简化示例时,我失去了对我正在尝试做的事情的一些解释,随后在您的一些回答中造成了混乱。对于那个很抱歉。我修改了代码以表明我必须将标签的多种可能性发送到函数。此外,每个对象都由一个标签和一个按钮指向。 (ps user3182143 - 不是我拒绝了你的答案)

标签: ios swift


【解决方案1】:

您似乎已将UIButton(在您的故事板或xib 上)连接为代码中的UILabel,并且您正试图将其视为标签。确保将 IB 上的按钮替换为标签,它将起作用。

【讨论】:

  • Xocode 不允许连接不匹配的类型作为插座。 xocde 不允许尝试将 unbutton 连接到 uilabel。我猜他已经正确连接了插座,但之后在代码中将 uibutton 的类型更改为 uilable。
  • 我假设我将无法做我正在寻找的事情并且会找到另一条路线。谢谢大家的cmets。
  • CNE 你试过我下面的答案了吗?如果对你有帮助,我会很高兴的。
  • 如果不需要将标签作为参数传递给 makeColor 函数,您的解决方案将有效。但是,我需要传递它,因为函数中的处理取决于选择了哪个按钮/标签。请查看我发布的带有完整代码的修改后的代码,这样我想要做什么就很明显了。谢谢
【解决方案2】:

不要传递局部变量

func makeColor()
{
    myLabel.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)

    print(myLabel.backgroundColor)
    myLabel.text = "4"
}

@IBAction func myButton(sender: AnyObject) {
   makeColor ()
}

现在如果你想为按钮设置标题

@IBOutlet var myButton: UIButton!

@IBAction func myButton(sender: AnyObject) 
{
    myButton.backgroundColor = UIColor(red: 0.1, green: 0, blue: 0, alpha: 0.1)
    myButton.titleLabel?.text = "4"
}

【讨论】:

    【解决方案3】:

    在来源(xib || 故事板)未知的情况下尝试这个来缓解错误

    extension UIButton
    {
        func setText(_ text: String) {
            assertionFailure("crashlytics reported Fatal Exception: NSInvalidArgumentException -[UIButton setText:]: unrecognized selector sent to instance 0xyyy objc_exception_throw        keyboard_arrow_down")
            titleLabel?.text = text
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2021-11-10
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多