我已经阅读了有关此问题的几个答案,但似乎找不到适用于我的代码的答案。当我分配“myLabel”的“text”成员(如下)时,我得到:“[UIButton setText:]:无法识别的选择器发送到实例......”程序编译正常,但在遇到赋值语句时崩溃。
但是我可以分配甚至打印出同一对象的背景颜色。关于我做错了什么有什么想法吗?
//******** 导入 UIKitclass 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/