初始化 UIAlertController 的子类

Initialising a subclass of UIAlertController

我想创建 UIAlertController 的自定义子类。 如果我理解正确,我需要在子类初始化期间的某个地方调用 super.init(title...

但我一直 运行 遇到 指定初始化器 的问题。我已阅读文档,但无法弄清楚如何让它工作。这是我的代码(注意代码中的注释):

class AccountInfoActionSheet: UIAlertController {

    required init?(coder aDecoder: NSCoder) { //xcode says I need this
        super.init(coder: aDecoder) //it also says I need to call a designated initializers so here it is
        super.init(title: "Info", message: "Heres the Info", preferredStyle: .actionSheet) //but now I can't call this initializer
        fatalError("init(coder:) has not been implemented")
    }
}

编辑:由于 UIAlertController 不能被子类化,我只是创建了一个函数,returns 在 ViewController 中正确配置了 UIAlertController需要。

正如limon's answer to another question所说,你不应该继承UIAlertController

根据 Apple's documentation:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

你不应该继承 UIAlertController

检查此 link:

https://developer.apple.com/documentation/uikit/uialertcontroller#//apple_ref/doc/uid/TP40014538-CH1-SW2

您可以使用 extensions 添加方法,但不应将其子类化。

如果您想要一种更简单的方法来创建通用警报控制器,您可以创建 UIAlertController 的扩展,而不是创建子类,它具有 return 已配置警报控制器的工厂方法你要的那个。例如:

extension UIAlertController {

    static func accountInfo() -> UIAlertController {
        // If you want to add Alert Actions you can add them to the controller before returning them.
        return UIAlertController(title: "Info", message: "Here's the Info", preferredStyle: .actionSheet)
    }
}

现在您可以简单地创建控制器:

let ac = UIAlertController.accountInfo()

如其他答案所述,您不应该继承 UIAlertController。作为一种选择,您可以使用工厂方法创建扩展:

extension UIAlertController {
    static func accountInfoActionSheet() -> UIAlertController {
        return UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
    }
}

但是,其他答案并不是特别真实地说你不能子类化 UIAlertController。 不应该,但如果你愿意,可以

UIAlertControllerUIViewController 的子类,因此它们具有相同的指定初始值设定项 init(nibName: String?, bundle: Bundle?).

你不应该在 init?(coder aDecoder: NSCoder) 中调用它,因为它本身就是一个指定的初始化程序。例如,当从故事板初始化控制器时会调用它。

下面是如何实现您想要的效果的示例(即使 Apple 不同意):

class AccountInfoActionSheet: UIAlertController {

    // preferredStyle is a read-only property, so you have to override it
    override var preferredStyle: UIAlertControllerStyle {
        return .actionSheet
    }

    init() {
        super.init(nibName: nil, bundle: nil)
        initialize()
    }

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

    // all of the configuration code that init methods might share
    // separated into a method for the sake of convenience
    private func initialize() {
        title = "Info"
        message = "Heres the Info"
        // any other setup needed
    }
}