如何将对象从UIViewController传递给Custom UIView

时间:2017-10-31 09:45:12

标签: ios swift

此AssetRFPViewController类在viewDidLoad中调用自定义视图(AssetBasicInfo),如果传递" Hello"它的确定,但是当我将对象传递给自定义视图(AssetBasicInfo)时,它没有被采用,我如何设计自定义视图类以使其捕获对象

class AssetRFPViewController: UIViewController
{
 var asset:Asset = Asset()
 override func viewDidLoad() {
        let customView = AssetBasicInfo(frame: self.topHeaderView.bounds)
        customView.assetNameValLbl.text = "Hello" // ok
        customView.assetObj = self.asset      // not ok, passing object
        self.topHeaderView.addSubview(customView)
       }
}

现在我的自定义类看起来像这样,

class AssetBasicInfo: UIView {
                @IBOutlet var containerView: UIView!
             var assetObj:Asset = Asset() // i want value in this object "assetObj"
            @IBOutlet var assetNameValLbl: UILabel!

            override init(frame: CGRect) {
                    super.init(frame: frame)
                    print(frame.size.width)
                    print(frame.size.height)

                    commonInit()
                }
 required init?(coder aDecoder: NSCoder) {
        super.init(coder : aDecoder)
        commonInit()
    }
        private func commonInit(){

                Bundle.main.loadNibNamed("HeaderViewXIB", owner: self, options: nil)
                self.addSubview(containerView)
                self.containerView.frame = self.frame
                containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]

                self.assetNameValLbl.text = assetObj.name // not setting value
                print("screenName" , screenName)
            }
    }

0 个答案:

没有答案