私はUIButtonをサブクラス化しようとしていますが、タイプが.systemであることが必要です。 「スーパークラスのUIButton "タイプUIButtonをsuclassingする。
答えて
の指定イニシャライザを呼び出す必要がありますが、コール・スーパークラスの指定イニシャライザする必要があります:
私は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を呼び出す必要がありますfスーパークラスのUIButton
しかし、それはself
を使用する前に、指定された初期化子に電話する必要があると言います。だからself.type
super.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")
}
}
を持つことができますすでにsuper.initを使用しています。私は問題がsuper.init(type:.system)が指定された初期化子ではないと考えています... – user3239711
@ user3239711申し訳ありませんが、私は質問を誤解しました。私は答えを更新しました。 – Rahul
あなたは便利なメソッドをオーバーライドして、あなたがFormButton
UIButtonType.system
の型を返す静的メソッドを行うことができます別の方法として、超便利なメソッド... を呼び出すことはできません。あなたは私のコードを見れば私は、
class FormButton: UIButton {
class func newButton() -> FormButton {
return FormButton.init(type: .system)
}
}
この
let button = FormButton.newButton()
それは私が吟味していたものです...ありがとう – user3239711
- 1. UIButtonのサイズ変更UIButtonのタイプを変更します
- 2. UIButtonタイプをプログラムで変更します
- 3. UIButtonタイプの色を設定する方法:SwiftのtvOSの.system
- 4. タイプ 'UIButton'の値を 'NSURL'にキャストできません
- 5. タイプUIButtonの値をキャストできませんでした:Swift
- 6. タイプ '(UIButton) - >()'の値にtableViewのメンバ 'setTitle'がありません
- 7. タイプ 'UIbutton'の値には 'titleforState'がありません
- 8. UIButtonイメージの前にUIButtonのタイトルを設定するには
- 9. UIButtonの右端にUIButtonイメージを配置する方法
- 10. UIButtonポジションを変更する
- 11. UIButtonをドラッグアンドドロップする方法
- 12. を表示するキーボードuibutton
- 13. ABTableViewCell - UIButtonを追加する
- 14. UITextfieldsをUIButtonでクリアする
- 15. UIButton
- 16. UIButtonイメージを別のUIButtonイメージビューに変更
- 17. タイプ 'UIButton'の値に '反応性のある'メンバーはありません
- 18. XcodeのUIButtonのXとYの位置はどのタイプですか?
- 19. UIButtonタイトルの切り替え: 'String'タイプの式パターンは 'String ?!'タイプの値と一致できません。
- 20. UIButtonを生成
- 21. はUIButton
- 22. uibuttonタイトル
- 23. UIButton(adjustsImageWhenHighlighted)
- 24. Round UIButton
- 25. カスタムUIButton
- 26. はUIButton
- 27. IBOutletCollection(UIbutton)
- 28. UIButtonが
- 29. UIButtonハートビートアニメーション
- 30. UIButtonは、
のようにそれを使用してくださいしかし、私はそれを行う場合、どのように私はタイプ.systemのUIButton(プロパティは読み取り専用です) – user3239711