Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios [UIButton setText:]:发送到实例的选择器无法识别_Ios_Swift - Fatal编程技术网

Ios [UIButton setText:]:发送到实例的选择器无法识别

Ios [UIButton setText:]:发送到实例的选择器无法识别,ios,swift,Ios,Swift,我已经阅读了关于这个问题的几个答案,但似乎找不到一个适用于我的代码的答案。当我分配“myLabel”(如下)的“text”成员时,我得到:“[UIButton setText:]:无法识别的选择器发送到实例…”程序编译正常,但遇到赋值语句时崩溃 然而,我能够指定,甚至打印出相同对象的背景色。你知道我做错了什么吗 //******** 导入UIKit class ViewController: UIViewController { //********* Button 1 *********/

我已经阅读了关于这个问题的几个答案,但似乎找不到一个适用于我的代码的答案。当我分配“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.

}

}
func makeColor(myLabel:UILabel,nextColor:Int)
{
开关nextColor
{
案例0:myLabel.backgroundColor=UIColor(红色:0.1,绿色:0,蓝色:0,alpha:0.1)
案例1:myLabel.backgroundColor=UIColor(红色:0.2,绿色:0,蓝色:0,alpha:0.2)
案例2:myLabel.backgroundColor=UIColor(红色:0.3,绿色:0,蓝色:0,alpha:0.3)
案例3:myLabel.backgroundColor=UIColor(红色:0.4,绿色:0,蓝色:0,alpha:0.4)
案例4:myLabel.backgroundColor=UIColor(红色:0.5,绿色:0,蓝色:0,alpha:0.5)
默认值:打印(“颜色结束”)
}
打印(myLabel.backgroundColor)

myLabel.text=“4”您似乎已将
UIButton
(位于故事板或xib上)连接为代码中的
UILabel
,并且您正试图将其视为标签。请确保将IB上的按钮替换为标签,它将工作。

不要传递局部变量

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"
}

如果来源(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
    }
}

还有一件事——按钮和标签都连接在一起。为了避免混淆,不要对实例和局部变量使用相同的名称(
myLabel
).按钮和标签连在一起是什么意思?有什么理由否决我的答案吗?我试过了,我把它放在这里。对不起,伙计们。我想当我试图简化这个例子时,我失去了一些对我试图做什么的解释,并随后在你的一些回答中造成了混乱。很抱歉。我已经修改了编辑代码以显示我必须向函数发送标签的多种可能性。此外,每个对象都由一个标签和一个按钮指向。(p.s.user3182143-不是我否决了你的答案)Xocode不允许将不匹配的类型连接为插座。xocde不允许尝试将解开的按钮连接到uilabel。我猜他已经正确连接了插座,但在这之后,在代码中将uibutton的类型更改为uilable。我假设我将无法执行我正在寻找的操作,并将找到另一条路径。谢谢大家r comments.CNE您是否尝试了我下面的答案?。如果对您有帮助,我将非常高兴。如果不需要将标签作为参数传递给makeColor函数,您的解决方案将有效。但是,我需要传递它,因为函数中的处理取决于选择的按钮/标签。请参阅我发布的修改代码有完整的代码,所以很明显我正在尝试做什么。谢谢