【发布时间】:2020-03-16 05:34:26
【问题描述】:
我拼命想了解为什么下面的实现不起作用。
我正在创建一个使用 Interface Builder 实现的父 ViewController,并且有一些连接的 @IBOutlets。
当我为这个 ViewController 创建一个子类时,我在运行时收到一个错误,说我所有的 @IBOutlets 都是 nil。
我尝试使用我的 init 方法“玩转”,看看如何确保我的 @IBOutlets 被初始化,但到目前为止没有运气。
任何人都知道如何使这种实现成为可能?
// Super class
class ScenesViewController: UIViewController {
// MARK: - IBOutlets
@IBOutlet weak var scenesCollectionView: UICollectionView!
@IBOutlet weak var topButtonLeft: UIButton!
@IBOutlet weak var topButtonRight: UIButton!
@IBOutlet weak var topButtonsStackView: UIStackView!
@IBOutlet weak var scenesPageControl: UIPageControl!
@IBOutlet weak var contentNestedStackView: UIStackView!
// MARK: - Init Method
convenience init(scenes: [Scene],
mainButtonTitle: String,
secondaryButtonTitle: String? = nil,
topButton: SceneTopButton? = nil,
bottomNavLinkTitle: String? = nil,
bottomNavLinkColoredText: String? = nil) {
self.init()
self.scenes = scenes
self.mainButtonTitle = mainButtonTitle
self.secondaryButtonTitle = secondaryButtonTitle
self.topButton = topButton
self.bottomNavLinkTitle = bottomNavLinkTitle
self.bottomNavLinkColoredText = bottomNavLinkColoredText
}
// Subclass
class OnboardingViewController: ScenesViewController {
convenience init() {
self.init() // Not working
self.scenes = SCENES.ONBOARDING // a constant object
self.mainButtonTitle = "mainButtonTitle"
self.secondaryButtonTitle = "secondaryButtonTitle"
self.topButton = .skip
self.bottomNavLinkTitle = "bottomNavLinkTitle"
self.bottomNavLinkColoredText = "Title"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupButton()
}
self.init() 方法显然创建了一个循环,所以它是不行的。但想不出如何正确实例化插座。
【问题讨论】: