【问题标题】:Using a Custom UIView for iCarousel为 iCarousel 使用自定义 UIView
【发布时间】:2016-04-17 12:38:43
【问题描述】:

我正在尝试使用iCarousel 来显示一系列项目。我想为此重用一个故事板 UIView,所以我创建了一个 UIView 的自定义类。我还为此创建了一个 XIB 文件,并根据自己的喜好设置了视图。然后,我将我的观点的出口连接到班级。我现在正在尝试重新创建 UIView,以便我可以将它用于我的轮播。这是我自定义的 UIView 类:

这是我的轮播代码:

func numberOfItemsInCarousel(carousel: iCarousel) -> Int {
    return costs.count
}

func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
    let rView = ReturnView(frame: CGRectMake(0, 0, 220, 220))
    rView.backgroundColor = UIColor.greenColor()
    print(rView.deleteBtn)//fatal error: Unexpectedly found nil when unwrapping an optional value
    return rView

}

从我的致命错误中可以看出,删除按钮不存在。这与我的其他元素相同。没有子视图出现:

如何让子视图出现在我的 iCarousel 上?感谢您的帮助。

【问题讨论】:

    标签: ios iphone swift uiview icarousel


    【解决方案1】:

    在你的轮播功能中试试这个

    {
    var myView : UIView
    if(myView != nil)
    {
     myView = view as! ReturnView    
    }
    else{
    var topLevelObjects : NSArray = NSBundle.mainBundle().loadNibNamed("your_xib_name", owner: self, options: nil)
    myView = topLevelObjects.objectAtIndex(0) as!  ReturnView
    }
    return myView }
    

    您需要在当前视图之上从您的 xib 加载视图。确保您已将您创建的自定义视图的所有者标记为要添加轮播的 PRESENT 视图控制器

    【讨论】:

      【解决方案2】:

      试试这个:

      rView.addSubview(myButtonName)
      

      对于“致命错误:展开可选值时意外发现 nil”,请尝试以下操作:

      if deleteBtn != nil{
      print(rView.deleteBtn)
      }
      

      我真的不明白您要做什么,“打印一个按钮?”但这可能是这样做的适当方式。希望这会有所帮助... :)))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多