ios - swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'

标签 ios iphone swift

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

'required' initializer 'init(coder:)' must be provided by subclass of '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!)
  }
}

这是类(class)问卷:

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]
  }
}

谢谢!

最佳答案

对于 required 的注意事项:在类初始值设定项的定义之前写入 required 修饰符,以指示该类的每个子类都必须实现该初始值设定项。

override 注意事项:在重写父类(super class)的指定初始化器时,您总是要写 override 修饰符,即使您的子类的初始化器实现是便利初始化器。

以上两个注释均来自:Swift Programming Language/Initialization

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

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    ...
}

关于ios - swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255466/

相关文章:

Swift 检查两个元素是否满足类型的组合

xcode - 如何从其他应用程序在 Facebook Messenger 上分享链接?

android - 如何减少 flutter 中的视频录制大小?

ios - 不常见的png文件iOS显示

当我构建并运行应用程序时,iPhone 模拟器屏幕是黑色的

cocoa-touch - QuickType 预测会考虑应该被我的 UITextFieldDelegate 阻止的击键

ios - 用于上传和下载文件的 iCloud 集成

iOS 应用内购买和销售应用内货币

ios - 使用 dispatch_after 做一些相对于其他事情何时完成的事情

iphone - 用于 iPhone 的 Objective-C DOM XML 解析器