我对Xcode很新,我正在研究这个代码,基本上,根据按下的按钮,会出现一个特定的标签。为了使它更简单(我不知道我最终会有多少个按钮和标签),我为标签和按钮创建了数组(我将按钮和标签拖动到代码作为Outlet Collection)。 无论如何,这是代码:
import UIKit
class ViewController: UIViewController {
//initialize everything
//labels first
@IBOutlet var labels: [UILabel]!
//buttons now
@IBOutlet var buttons: [UIButton]!
//pictures next
@IBOutlet var pic: UIImageView!
//function for all buttons
@IBAction func button_action(_ sender: UIButton) {
let propertyToCheck = sender.tag
for i in 0...buttons.count-1{
if buttons[i].tag != propertyToCheck{
labels[i].isHidden = true
}
else{
labels[i].text = "The right text"
if labels[i].isHidden == true {
labels[i].isHidden = false
} else {
labels[i].isHidden = true
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var index = 0
//initialize view
//labels
for label in labels{
label.isHidden = true
}
//pics
pic.isHidden = true
//associate action for every button
for button in buttons {
button.tag = index// setting tag, to identify button tapped in action method
print("Hello", button.tag, buttons.count)
button.addTarget(self, action: #selector(button_action(_:)), for: UIControlEvents.touchUpInside)
index = index + 1
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我收到以下错误:“libc ++ abi.dylib:以NSException类型的未捕获异常终止”,当我查看AppDelegate时,我发现:“线程1:信号SIGABRT”。这是层次结构:
同样,我很新,所以我猜这是一个错误的调用不存在的东西或连接的东西,但我无法弄清楚它是什么!我已经在网上寻找可能的解决方案,比如清理和重建项目,但到目前为止还没有任何工作。
任何建议都会非常感激!