1
votes

I am trying to use iCarousel to display a series of items. I wanted to reuse a storyboard UIView for this, so I created a custom class of a UIView. I also created an XIB file for this, and I set up the view to my preference. I then hooked up the outlets of my view to the class. I am now trying to recreate a UIView so I can use it for my carousel. Here is my custom class of UIView:

Custom Class

Here is my code for my Carousel:

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

}

As you can see from my fatal error, delete button does not exist. This is the same with the rest of my elements. None of the subviews appear:

Subviews

How can I get the subviews to appear on my iCarousel? Thanks for your help.

2

2 Answers

0
votes

try this in your carousel function

{
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 }

You need to load the view from ur xib on top of current view. Make sure u have marked the owner of Custom view u have created as PRESENT view controller where you are adding carousel

0
votes

try this:

rView.addSubview(myButtonName)

and for the 'fatal error: Unexpectedly found nil when unwrapping an optional value' try this:

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

I truly don't understand what you are trying to do, "printing a button?" but this might be an appropriate way of doing this. Hope this helps... :)))