2016-02-19 6 views
7
class CustomView: UIView { 

    var subViewColor:UIColor 
    var subViewMessage:String 

    override init(frame:CGRect) { 
     super.init(frame:frame) 
    } 

    init(subViewColor:UIColor,subViewMessage:String){ 

     self.subViewColor = subViewColor 
     self.subViewMessage = subViewMessage 
     super.init() 

    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

} 

Mam klasy gdzie chcę użytkownik może zainicjować widoku niestandardowego albo poprzez nadanie właściwości jak:Zastąp metoda init UIView w szybkim

let myView = CustomLoadingView(initialize properties here) 

Jeśli użytkownik nie chce zainicjować własną rękę właściwości, chcę zainicjować CustomLoadingView użyciu właściwości domyślnych ...

let myView = CustomLoadingView() // this should initialize using default value 

jednak z tego, otrzymuję ten błąd:

Must call a designated intializer of the superclass UIView

Odpowiedz

26

W init(subviewColor: UIColor, subViewMessage: String) nie wywołujesz wskazanego inicjatora (jak ładnie wskazuje kompilator).

Jeśli nie wiesz, jakie są określone inicjalizatory, są one inicjatorami, które mają, aby mogły zostać wywołane przez podklasę w pewnym momencie. Od docs:

Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.

W tym przypadku, wyznaczony initializer dla UIView jest init(frame: CGRect), czyli w pewnym momencie, nowy inicjator init(subviewColor: UIColor, subViewMessage: String musi wywołać super.init(frame:).

W celu ustalenia tego, wprowadzić następujące zmiany:

init(frame: CGRect, subViewColor: UIColor, subViewMessage: String){ 

    self.subViewColor = subViewColor 
    self.subViewMessage = subViewMessage 
    super.init(frame: frame) 

} 

lub można zadzwonić do innego inicjatora w swojej klasie, który kończy się wywołaniem wyznaczony inicjatora.

override init(frame: CGRect) { 
    super.init(frame: frame) // calls designated initializer 
} 

convenience init(frame: CGRect, subViewColor: UIColor, subViewMessage: String){ 

    self.subViewColor = subViewColor 
    self.subViewMessage = subViewMessage 
    self.init(frame: frame) // calls the initializer above 

} 

chodzi o metodę wygodę prostu CustomLoadingView(), trzeba dodać inny inicjator za to. Dodaj ten kod do widoku niestandardowego:

convenience init() { 
    self.init(frame: DEFAULT_FRAME, subViewColor: DEFAULT_COLOR, subViewMessage: DEFAULT_MESSAGE) 
} 

Jeśli chcesz dowiedzieć się więcej o wyznaczonych i wygoda inicjalizatorów, przeczytaj o nich here i here.

+0

Działa to tylko dla mnie, jeśli nazywam 'self.init (frame: frame)' jako pierwszą rzeczą w 'convenience init' metoda – Joseph

4

Spróbuj tego:

class CustomView: UIView { 

    var subViewColor:UIColor 
    var subViewMessage:String 

    init(subViewColor:UIColor,subViewMessage:String){ 

     self.subViewColor = subViewColor 
     self.subViewMessage = subViewMessage 

     let frame = self.frame 
     //Or you can use custom frame. 

     super.init(frame: frame) 

    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 
+2

Powoduje błąd kompilatora: _'self 'użyte przed super.initem call_. [Zobacz tę odpowiedź.] (Https://stackoverflow.com/a/33765813/629407) – dxb

0

Musisz zadzwonić do jednego z wyznaczonych inicjalizatorów UIView jest w pewnym momencie w niestandardowym inicjatora, na przykład: super.init(frame: frameX). Twój numer super.init() nie spełnia tego wymagania.

Powiązane problemy