我有四個最初是可選的UIView對象。然後,當我加載視圖時,它們被放入一個數組中,並在for循環中爲它們分配一個UIStackView。但是當我檢查對象的全局實例是否等於數組中的一個時,它不是。我現在很困惑。Swift 3:通過遍歷視圖數組設置視圖
//global
@IBOutlet weak var buttonA: UIButton!
@IBOutlet weak var buttonB: UIButton!
@IBOutlet weak var buttonC: UIButton!
@IBOutlet weak var buttonD: UIButton!
var answerAView: UIView?
var answerBView: UIView?
var answerCView: UIView?
var answerDView: UIView?
override func viewDidLoad() {
var answersViews = [self.answerAView, self.answerBView, self.answerCView, self.answerDView]
var buttons = [buttonA, buttonB, buttonC, buttonD]
var answers = ["A", "B", "C", "D"]
var count = 0
for a in answers {
let newStack = UIStackView()
newStack.axis = .vertical
let label = UILabel()
label.textColor = UIColor.black
label.text = a
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
newStack.addArrangedSubview(label)
newStack.distribution = .fillEqually
newStack.spacing = 10
NSLayoutConstraint(item: buttons[count], attribute: .width, relatedBy: .equal, toItem: rows[count], attribute:.width, multiplier: 0.15, constant:0.0).isActive = true
answersViews[count] = newStack
if answerAView == answersViews[count] {
print("Settkekasldfjkskdjblvkjbalkvbksjbvb")//this does not print
}
count += 1
}
}
看來你的代碼中有語法錯誤少明智......請充分後會source..it是有益的 –