CNE
CNE

Reputation: 63

[UIButton setText:]: unrecognized selector sent to instance

I have read several answers on this question but can't seem to find one that applies to my code. When I assign the "text" member of "myLabel" (below) I get: "[UIButton setText:]: unrecognized selector sent to instance...." The program compiles OK but then crashes when the assignment statement is encountered.

However I am able to assign and even print out the backgroundColor of the same object. Any ideas on what I am doing wrong?

//******** import 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.

}

}

Upvotes: 2

Views: 2591

Answers (3)

Anton Tropashko
Anton Tropashko

Reputation: 5816

try this to ease the bug in case the origin (xib || storyboard) is unknown

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

Upvotes: 0

user3182143
user3182143

Reputation: 9609

Don't pass the local variable

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 ()
}

Now If you want to set the title for button

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

Upvotes: 0

Fahri Azimov
Fahri Azimov

Reputation: 11770

It seems like you have connected the UIButton(which is on your storyboard or xib) as as UILabel in the code, and you are trying to treat it as label. Make sure to replace the button on the IB to label, and it will work.

Upvotes: 3

Related Questions