0

我正在尝试convenience init为 subclassed 创建一个没有参数的UIAlertController,但它给了我一个错误。

它给了我这个错误:

在调用 self.init 之前在委托初始化程序中使用 self

class AvatarSelectionAlert: UIAlertController {

    convenience init(test: String) {
        let title = NSLocalizedString("ALERT_CHOOSE_AVATAR_TITLE", comment: "")
        let message = NSLocalizedString("ALERT_CHOOSE_AVATAR_MESSAGE", comment: "")
        self.init(title: title, message: message, preferredStyle: .ActionSheet)

        let selectPremadeAvatarAction = UIAlertAction(title: NSLocalizedString("SELECT_PREMADE_AVATAR", comment: ""), style: .Default) { Void in

        }
        addAction(selectPremadeAvatarAction)

        let chooseFromGalleryAction = UIAlertAction(title: NSLocalizedString("CHOOSE_FROM_GALLERY", comment: ""), style: .Default) { Void in

        }
        addAction(chooseFromGalleryAction)

        let takeNewPictureAction = UIAlertAction(title: NSLocalizedString("TAKE_NEW_PICTURE", comment: ""), style: .Default) { Void in

        }
        addAction(takeNewPictureAction)

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { Void in
        }
        addAction(cancelAction)
    }
}

我也曾尝试self.init()在打电话给对方之前打电话self.init,但它崩溃了。

是否可以创建convenience init没有参数的?

4

2 回答 2

0

你想在子类化 UIAlertController 中实现什么?

子类化注释

UIAlertController 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。参考这里

于 2016-02-19T07:52:09.357 回答
0

只需做 UIAlertController 的扩展。

extension UIAlertController {

    public convenience init?(test: String) {
        let title = NSLocalizedString("ALERT_CHOOSE_AVATAR_TITLE", comment: "")
        let message = NSLocalizedString("ALERT_CHOOSE_AVATAR_MESSAGE", comment: "")
        self.init(title: title, message: message, preferredStyle: .actionSheet)

        let selectPremadeAvatarAction = UIAlertAction(title: NSLocalizedString("SELECT_PREMADE_AVATAR", comment: ""), style: .default) { Void in

        }
        addAction(selectPremadeAvatarAction)

        let chooseFromGalleryAction = UIAlertAction(title: NSLocalizedString("CHOOSE_FROM_GALLERY", comment: ""), style: .default) { Void in

        }
        addAction(chooseFromGalleryAction)

        let takeNewPictureAction = UIAlertAction(title: NSLocalizedString("TAKE_NEW_PICTURE", comment: ""), style: .default) { Void in

        }
        addAction(takeNewPictureAction)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { Void in
        }
        addAction(cancelAction)
    }

}
于 2017-08-25T07:34:50.827 回答