【问题标题】:Swift: how to create a convenience init for UIAlertController with no parametersSwift:如何为没有参数的 UIAlertController 创建一个方便的 init
【发布时间】:2016-02-19 07:15:13
【问题描述】:

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

它给了我这个错误:

在调用 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

【问题讨论】:

  • 检查你的方法 self.init(title: title, message: message, preferredStyle: .ActionSheet) 是否在调用 super.init?
  • 用我的完整方法更新。当我在另一个 self.init(title, message, style) 之前调用 self.init() 时,它会在 self.init() 行上崩溃
  • 是的,您可以创建一个不带参数的便捷初始化。
  • @Prabhu.Somasundaram 如果我在另一个初始化程序之前添加self.init(),则会出现运行时错误

标签: ios swift uialertcontroller convenience-methods


【解决方案1】:

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

子类化注释

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

【讨论】:

    【解决方案2】:

    只做 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)
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      相关资源
      最近更新 更多