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'. 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)
}
}
Instead, it's unsafe to override a convenience initializer — in the context of all the other rules about designated and convenience initializers, and the two-passes of initializations, and order in which initialization occurs. Many parts of the Swift language make safety guarantees. This is one of them.
you are not overriding any convenience init, it looks like you are creating a new one.
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:
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.
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:
Important
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.
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.
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. The rest of the magic is up to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With