简体   繁体   English

无法以编程方式将视图添加到情节提要中的现有子视图

[英]Can't add a view to an existing subview in storyboard programatically

I'm trying to simulate this view hierarchy in code: 我正在尝试在代码中模拟此视图层次结构:

 "scene"
    "view Shop Main"                       -> `UIView`
        "view shop scroll"                 -> `UIScrollView`
            "ViewDetail"                   -> `UIView`
                "Shop Connected Container" -> `UIView`
                    "Connected Shop"       -> `UIView`

I want to instantiate multiples "Connected Shop" and add them to "Shop Connected Container" So like this: 我想实例化多个“ Connected Shop”并将其添加到“ Shop Connected Container”,如下所示:

"scene"
    "view Shop Main"                       -> `UIView`
        "view shop scroll"                 -> `UIScrollView`
            "ViewDetail"                   -> `UIView`
                "Shop Connected Container" -> `UIView`
                    "Connected Shop"       -> `UIView`
                    "Connected Shop"       -> `UIView`
                    "Connected Shop"       -> `UIView`

How can I do this programatically?? 如何以编程方式执行此操作? I have a custom class for each subView and a xib file as well. 我为每个subView和一个xib文件都有一个自定义类。 This is how I'm instantiate the subviews from the code: 这就是我从代码中实例化子视图的方式:

  override func viewDidLoad() {

  /*1*/         let vShopMain = NSBundle.mainBundle().loadNibNamed("viewShopMain", owner: self, options: nil).first as! viewShopMain
    /*2*/       let vShopScroll = NSBundle.mainBundle().loadNibNamed("viewShopScroll", owner: vShopMain, options: nil).first as! viewShopScroll

    /*3*/       let vDetail : UIView = NSBundle.mainBundle().loadNibNamed("viewDetail", owner: vShopScroll, options: nil).first as! viewDetail
//
//
    /*4*/       let sConContainer : UIView = NSBundle.mainBundle().loadNibNamed("shopConnectedContainer", owner: vDetail, options: nil).first as! shopConnectedContainer
   /*5*/        let cShop = NSBundle.mainBundle().loadNibNamed("connectedShop", owner: sConContainer, options: nil).first as! connectedShop


        cShop.bannerTitle.text = "Shop Title"

        vShopMain.addSubview(vShopScroll)
        vShopScroll.addSubview(vDetail)
        vDetail.addSubview(sConContainer)
        sConContainer.addSubview(cShop)


        self.view.addSubview(sConContainer)

Also tried this with the exact same result: 还尝试了完全相同的结果:

        let vShopMain = UINib(nibName: "viewShopMain", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! viewShopMain

       let scroll = UINib(nibName: "viewShopScroll", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! viewShopScroll

        let vDetail = UINib(nibName: "viewDetail", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! viewDetail

        let sConContainer = UINib(nibName: "shopConnectedContainer", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! shopConnectedContainer

        let cShop = UINib(nibName: "connectedShop", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! connectedShop

        cShop.bannerTitle.text = "hola"

        vShopMain.addSubview(scroll)
        scroll.addSubview(vDetail)
        vDetail.addSubview(sConContainer)
        sConContainer.addSubview(cShop)


        self.view.addSubview(sConContainer)

But the "new" views added are below the other view, which is not the intended behavior, since I'm instantiating existing views from storyboard, not creating new ones. 但是添加的“新”视图位于另一个视图的下面,这不是预期的行为,因为我是从情节提要中实例化现有视图,而不是创建新视图。

I'm following the order of the storyboard hierarchy. 我遵循情节提要层次结构的顺序。 Also the views are linked with their custom classes, and they have their xib files. 这些视图也与它们的自定义类链接在一起,并且具有其xib文件。 But when I run my app I got this weird issue 但是当我运行我的应用程序时,我遇到了这个奇怪的问题

What am I doing wrong here? 我在这里做错了什么? Please help 请帮忙

Help you to figure out why its crashing. 帮助您找出其崩溃原因。 loadNibNamed returns an array of objects not a single view. loadNibNamed返回对象数组,而不是单个视图。 So this will definitely crash 所以这肯定会崩溃

let vShopScroll = NSBundle.mainBundle().loadNibNamed("viewShopScroll", owner: self, options: nil) as! viewShopScroll  

since you are assuming it returns viewShopScroll and forcefully unwrapping it. 因为您假设它返回viewShopScroll并强制展开。 As you said your first line is working since there you taking the first object 正如您所说的那样,自从您取走第first物体以来,第一行一直在工作

let vShopMain = NSBundle.mainBundle().loadNibNamed("viewShopMain", owner: self, options: nil).first as! viewShopMain

Do the same for all other cases as well 其他所有情况也一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM