Curnelious Curnelious - 4 months ago 14
Swift Question

Init a UIView costume class with some data?

Right now my UIView class looks like this :

class CostumFlowViewCell: UIView {
override init (frame : CGRect)
{

super.init(frame : frame)

}

convenience init () {
self.init(frame:CGRect.zero)
}

required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}


I would like to be able to send it some array when I initialise it, so right now for example, I add it with :

let cell: CostumFlowViewCell= CostumFlowViewCell(frame:CGRectMake(0,0,0,0)
self.addSubView(cell)


How would I init it with some
NSArray
? (inside the cell class , and when I add it)

Answer

You can add an initializer like this:

init(anArray: NSArray)
{
    super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    //do something with anArray
}

If you want the ability to init with an array and a frame, you can do so:

init(anArray: NSArray, frame: CGRect)
{
    super.init(frame: frame)
    //do something with anArray
}
Comments