我对 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”。这是层次结构:
同样,我很新,所以我猜这是调用不存在的东西或有连接的东西的错误,但我不知道它是什么!我已经在网上寻找可能的解决方案,例如清理和重建项目,但到目前为止没有任何效果。
任何建议将不胜感激!