2017-05-08 23 views
-1

我有四個最初是可選的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 
    } 
} 
+0

看來你的代碼中有語法錯誤少明智......請充分後會source..it是有益的 –

回答

0

可能是你應該寫如下

//global 
    var answerAView: UIView? 
    var answerBView: UIView? 
    var answerCView: UIView? 
    var answerDView: UIView? 

    //inside viewDidLoad 
    var answersViews = [answerAView, answerBView, answerCView, answerDView] 

    for view in answersViews { 
     let stackView = UIStackView() 
     stackView.addSubview(view) 
     print("success") 
    } 
+0

我更新的代碼,因爲你可以看到我需要的uiview成爲堆棧視圖 – Filipe

+0

您編寫的代碼爲「answersViews [count] = newStack 」是錯的 – pusswzy

+0

爲什麼當我使用@IBOutlet弱變量answerA:UITextView!?如answerAview = answerA。 – Filipe