即使搜索结果,我也无法了解如何继续。我是xcode 7的新手。
目前已创建一个由一个视图组成的应用程序,其中一个图像用作按钮和一个标签。标签显示默认文本。
我还创建了一个包含多个名称的数组。单击该按钮时,将从数组中随机选择一个名称并显示在标签中。
所有这一切都运行正常,但再次单击该按钮时,应用程序崩溃......
错误代码如下
2016-01-28 20:25:07.705 Coffee Randomizer.temp_caseinsensitive_rename[3434:361389] -[UIButton setText:]: unrecognized selector sent to instance 0x7fa2cacd40b0
2016-01-28 20:25:07.719 Coffee Randomizer.temp_caseinsensitive_rename[3434:361389] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]: unrecognized selector sent to instance 0x7fa2cacd40b0'
我想我需要做一些事情来刷新标签或其他东西,但似乎无法掌握它。
使用代码
更新OP//
// ViewController.swift
// Coffee Randomizer
//
import UIKit
class ViewController: UIViewController {
// MARK: add both the elements.
@IBOutlet weak var coffeeButton: UIButton!
@IBOutlet weak var coffeeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
// Not sure but this should be an array with possible names to select
var theCoffeeGuys: [String] = ["User1", "User2", "User3"]
@IBAction func setLabel(sender: UIButton) {
let randomIndex = Int(arc4random_uniform(UInt32(theCoffeeGuys.count)))
coffeeLabel.text = theCoffeeGuys[randomIndex];
}
}
答案 0 :(得分:1)
'-[UIButton setText:]: unrecognized selector sent to instance
您正在尝试将setText:
消息发送到UIButton
的实例。有几个UIView
子类具有text
属性,并会响应setText:
(例如UILabel
),但UIButton
不是其中之一。
不幸的是为什么你将这条消息发送到按钮而不是你想要的任何视图仍然是个谜,直到你展示你的实现。