2017-05-24 2 views

答えて

0

の指定イニシャライザを呼び出す必要がありますが、コール・スーパークラスの指定イニシャライザする必要があります:

私は

class FormButton: UIButton { 

var type FormButtonType: FormButtomType.oneSelection 

init(oftype formType: FormButtomType) { 
    self.type = formType 
    super.init(type: .system) 
} 

} 

問題は、私は次のエラーメッセージを持っているということです初期化子に苦しんでいます

init(oftype formType: FormButtomType) { 

    super.init(frame: yourframe) 
    self.type = formType 

} 
+0

のようにそれを使用してくださいしかし、私はそれを行う場合、どのように私はタイプ.systemのUIButton(プロパティは読み取り専用です) – user3239711

0

エラーは少し紛らわしい

が指定イニシャライザ0を呼び出す必要がありますfスーパークラスのUIButton

しかし、それはselfを使用する前に、指定された初期化子に電話する必要があると言います。だからself.typesuper.initコールの後にコールしてください。超過コールを必要としないconvenienceイニシャライザを作成した場合は、selfに電話する必要があります。

まず、この行は構文的に間違っています。

var type FormButtonType: FormButtomType.oneSelection 

は今、あなたは簡単にそれをサブクラス化することができます

var type: FormButtonType = FormButtomType.oneSelection 

なるべきです。

import UIKit 

class FormButton: UIButton { 

    var type: FormButtonType = FormButtomType.oneSelection 

    // convenence initializer, simply call self if you have 
    // initialized type 
    convenience init(type: UIButtonType) { 
     self.init(type: type) 
    } 

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

は今、あなたは、あなたがoverride指定イニシャライザにする必要がありますデフォルトまたは任意の値を持っていないいくつかのプロパティを初期化したい場合。

例えば、

class FormButton: UIButton { 

    var type: UIButtonType = UIButtonType.system 
    var hello: String // property doesn't have initial value 

    // This is designated initialiser 
    override init(frame: CGRect) { 
     self.hello = "Hello" 

     super.init(frame: .zero) 
    } 

    convenience init(type: UIButtonType) { 
     self.init(type: .system) 
    } 

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

を持つことができますすでにsuper.initを使用しています。私は問題がsuper.init(type:.system)が指定された初期化子ではないと考えています... – user3239711

+0

@ user3239711申し訳ありませんが、私は質問を誤解しました。私は答えを更新しました。 – Rahul

1

あなたは便利なメソッドをオーバーライドして、あなたがFormButtonUIButtonType.systemの型を返す静的メソッドを行うことができます別の方法として、超便利なメソッド... を呼び出すことはできません。あなたは私のコードを見れば私は、

class FormButton: UIButton { 
    class func newButton() -> FormButton { 
     return FormButton.init(type: .system) 
    } 
} 

この

let button = FormButton.newButton() 
+0

それは私が吟味していたものです...ありがとう – user3239711

関連する問題