简体   繁体   English

如何在UIAlertController中为swift覆盖便利init?

[英]How can I override convenience init in UIAlertController for swift?

I'm trying to override convenience init in UIAlertController, but it gives me an error like 'initializer does not override a designated initializer from its superclass'. 我试图覆盖UIAlertController中的便利init,但它给我一个错误,比如'初始化程序不会覆盖其超类的指定初始化程序'。 How can I override it using inheritance or extension whatever? 我怎样才能使用继承或扩展来覆盖它呢? My codes are below. 我的代码如下。

import UIKit

class ColorAlertViewController: UIAlertController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, colorCode: String?){
        super.init(title: title, message: message, preferredStyle: preferredStyle)
    }
}

you are not overriding any convenience init, it looks like you are creating a new one. 你没有覆盖任何方便的init,看起来你正在创建一个新的。

convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, colorCode: String?){
    self.init(title: title, message: message, preferredStyle: preferredStyle)
}

Is probably what you want, you just need to handle the color code 可能是你想要的,你只需要处理颜色代码

Looks like you are going to have to go a round about way: 看起来你将不得不绕道而行:

First create a create extension 首先创建一个创建扩展

extension UIAlertController
{

    class func create(title: String?, message: String?, preferredStyle: UIAlertControllerStyle) -> AnyObject
    {
        return UIAlertController(title: title, message: message, preferredStyle: preferredStyle);
    }
}

Then in the ColorAlertViewController, you will create another function to create this object: 然后在ColorAlertViewController中,您将创建另一个函数来创建此对象:

class func createWithColor(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, colorCode: String?) -> AnyObject
{
    var c = super.create(title, message: message, preferredStyle: preferredStyle);
    //handle color code here
    return c;
}

Now anywhere you want to create this object, just call 现在,无论您想要创建此对象,只需调用即可

var colorAlertView = ColorAlertViewController.createWithColor("title Name", message: "the message", preferredStyle: .ActionSheet, colorCode: "the color");

of course this won't work inside the UI builder, you would have to create this via code. 当然,这在UI构建器中不起作用,您必须通过代码创建它。

You really should be wrapping this inside a class and not overriding. 你真的应该将它包装在一个类中而不是重写。 That way your wrapper class can perform activities hidden from the caller. 这样,您的包装器类可以执行从调用者隐藏的活动。

Apple does clearly state that UIAlertViewController is not to be inherited from: Apple确实声明UIAlertViewController不会继承自:

Important 重要

The UIAlertController class is intended to be used as-is and does not support subclassing. UIAlertController类旨在按原样使用,不支持子类化。 The view hierarchy for this class is private and must not be modified. 此类的视图层次结构是私有的,不得修改。

Implementation: 执行:

extension UIAlertController
{
    class func create(title: String?, message: String?, preferredStyle: UIAlertControllerStyle) -> AnyObject
    {
        return UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert);
    }
}

class ColorAlertController {
    private var alertController: UIAlertController!

    private init() {
    }

    public convenience init(title: String?, message: String?, initialValue: T) {
        self.init()
        alertController = UIAlertController.createAlert(title: title, message: message)
        ... do color stuff ...
    }
}

The ColorAlertController can also include some modal presentation functions. ColorAlertController还可以包含一些模态演示功能。

func present(inViewController controller: UIViewController) {
    controller.present(alertController, animated: true)
}

You can do some pretty cool things with wrapping UIAlertControllers inside custom modal controllers like this. 你可以把UIAlertControllers包装在像这样的自定义模态控制器中,做一些非常酷的事情。 The rest of the magic is up to you. 其余的魔力取决于你。

There seems to be no way to do this "override". 似乎没有办法做这个“覆盖”。

According to this answer , it seems that you need to "override" the origin init before adding you custom init : 根据这个答案 ,看来你需要“越权”的由来init添加您的自定义前init

class ColorAlertViewController: UIAlertController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle) {
        self.init(title: title, message: message, preferredStyle:preferredStyle)
    }

    convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, colorCode: String?){
        self.init(title: title, message: message, preferredStyle: preferredStyle)
        // handle colorCode
    }

}

This code has no compile error. 此代码没有编译错误。 However this cause infinite loop at runtime. 但是这会在运行时导致无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM