Swift:错误:'required'初始化程序'init(编码器:)'必须由'UIView'的子类提供

R1'*_*R1' 12 iphone ios swift

当我在swift 2中构建我的应用程序时遇到问题.Xcode说:

'required'initulator'init(coder :)'必须由'UIView'的子类提供

这是该类的代码:

class creerQuestionnaire: UIView {
  @IBOutlet weak var nomQuestionnaire: UITextField!
  @IBOutlet weak var question: UITextField!
  @IBOutlet weak var reponse: UITextField!
  var QR: Questionnaire

  @IBAction func creerQuestion(sender: AnyObject) {
    QR.ajouterQuestion(question.text!, nouvReponse: reponse.text!)
  }
}
Run Code Online (Sandbox Code Playgroud)

这是班级调查问卷:

import Foundation

class Questionnaire {
  var QR = [String(), String()]

  func getQuestion(nbQuestion: Int) ->String {
    return QR[nbQuestion]
  }

  func getReponse(nbReponse: Int) ->String {
    return QR[nbReponse]
  }

  func ajouterQuestion(nouvQuestion: String, nouvReponse: String) {
    QR += [nouvQuestion, nouvReponse]
  }
}
Run Code Online (Sandbox Code Playgroud)

留言Merci!

All*_*len 20

需要注意:在类初始化程序的定义之前写入必需的修饰符,以指示该类的每个子类都必须实现该初始化程序.

覆盖注意事项:当覆盖超类指定的初始化程序时,总是编写覆盖修饰符,即使您的子类的初始化程序的实现是一个便利初始化程序.

以上两个注释均参考:Swift编程语言/初始化

因此,您的UIView的子类应该类似于下面的示例:

class MyView: UIView {
    ...
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)