问题
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)
}
}
回答1:
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.
回答2:
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.
回答3:
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
:
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.
来源:https://stackoverflow.com/questions/33092628/how-can-i-override-convenience-init-in-uialertcontroller-for-swift