Swift:从[Int:UIView]()访问UIView子级

时间:2018-07-02 11:02:12

标签: swift uiview uiscrollview

我使用以下代码将view添加到scrollview并将view保存到viewAll

var viewAll = [Int: UIView]()

i = 0
for pack in packs as! [[String: AnyObject]] {
    let packId = Int(pack["packId"] as! String)
    let packTitle = pack["packTitle"] as! String

    let view = CategoryClass.createMyClassView()
    view.pcId.text = packId
    view.pcTitle.text = packTitle

    viewAll[i] = view
    i = i+1

    myScrollView.addSubview(view)
}

工作正常。

现在,我想再次访问视图的子项:

for view in viewAll {
    view.value.pcId.text = "TEST"  
}

此行不起作用:view.value.pcId.text = "TEST"

CategoryClass:

class CategoryClass: UIView {
    @IBOutlet weak var pcId: UILabel!
    @IBOutlet weak var pcTitle: UILabel!

    class func createMyClassView() -> CategoryClass {
        let myClassNib = UINib(nibName: "Category", bundle: nil)
        return myClassNib.instantiate(withOwner: nil, options: nil)[0] as! CategoryClass
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为投射,您需要更改

false

var viewAll = [Int: UIView]()

//

也更改此

var viewAll = [Int: CategoryClass]()

view.value.pcId.text = "TEST"