ios - How to init custom UIView in Swift 1.2 -


i wish init uiview subclass programmatically, should initiated with, say, int. i'm looking this:

class onefingerview: uiview {     let id: int      init(id: int)     {         self.id = id         super.init()     }      required init(coder adecoder: nscoder)     {         super.init(coder: adecoder)     } } 

but error

must call designated initializer of superclass uiview

my attempt before follows:

class onefingerview: uiview {     let id: int     var delegate: onefingerviewprotocol? = nil      init(id: int, frame: cgrect)     {         self.id = id         super.init(frame: frame)     }      override init(frame: cgrect)     {         super.init(frame: frame)     } } 

but error have not implemented init(coder:).

how do this? there seems endless cycle of various errors no matter approach take.

override initwithframe , call super.init(frame:)

i misspoke in comment. need implement init(coder:) in method won't able provide id parameter, , need set somewhere else. might @ making id property ibinspectable , setting in ib.

the init(coder:) method designed save properties want custom subclass. override both init(coder:) , encodewithcoder(). in methods you'd call super version, , load/save property in custom code (using methods in nskeyedunarchiver , nskeyedarchiver.)

however, init(coder:) , encode(coder:) aren't useful objects create in ib, because ib doesn't set custom properties you. better use vanilla init(coder:) calls super, , configure objects in code, or ibdesignable properties, mentioned above.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -