应用程序尝试以模态方式呈现活动控制器自定义UI Alert Swift 3

时间:2018-04-22 09:00:42

标签: ios swift xcode uialertview viewcontroller

它可以在90%的时间内正常工作但

我在不同情况下提出def recursive_sum(l): result = 0 for i in l: if type(i) is list: result += recursive_sum(i) elif type(i) is int: result += i return result 后出现此错误

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:'应用程序试图呈现   modally一个活动控制器,

以下是自定义alertController

的课程
alertController

这里是类警报消息中显示的字体函数

class AlertMessage {


    private var alertController:UIAlertController!

    static let ATTRIBUTED_TITLE = "attributedTitle"
    static let ATTRIBUTED_MESSAGE = "attributedMessage"
    static let tintColor =  UIColor(rgb: 0xFDC32E)


    /// setup and configure the font for title
    ///
    /// - Parameter title: the title for alert message
    /// - Returns: attributed strings
    static func attributed(title:String) -> NSMutableAttributedString{
        return NSMutableAttributedString(string: title, attributes: Tools.setFontItemBy(size: 20))
    }

    /// setup and configure the font for message
    ///
    /// - Parameter message: message for user
    /// - Returns: attributed strings
    static func attributed(message:String) -> NSMutableAttributedString {
        return NSMutableAttributedString(string: message, attributes: Tools.setFontItemBy(size: 15))
    }


    private init(newTitle:String) {
        self.alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
        self.alertController.setValue(AlertMessage.attributed(title:newTitle), forKey: AlertMessage.ATTRIBUTED_TITLE)
        self.alertController.view.tintColor = AlertMessage.tintColor
    }

    convenience init() {
        self.init(newTitle: Texts.WARNING)
        self.alertController.addAction(UIAlertAction(title:Texts.YES, style: .default, handler: nil))
    }

     convenience init (title:String){
        self.init(newTitle: title)
        self.alertController.addAction(UIAlertAction(title: Texts.YES, style: .default, handler: nil))
    }


    convenience init (title:String, completion:@escaping ((UIAlertAction) -> (Swift.Void))){
        self.init(newTitle: title)
        self.alertController.addAction(UIAlertAction(title:Texts.YES, style: .default, handler: completion))
    }

    convenience init (title:String, message:String, completion:@escaping ((UIAlertAction) -> (Swift.Void))){
        self.init(newTitle: title)
        self.alertController.setValue(AlertMessage.attributed(message:message), forKey: AlertMessage.ATTRIBUTED_MESSAGE)
        self.alertController.addAction(UIAlertAction(title:Texts.YES, style: .default, handler: completion))
    }

    convenience init (title:String,
          completionAccept:@escaping ((UIAlertAction) -> (Swift.Void)),
          completionRefuse:@escaping ((UIAlertAction) -> (Swift.Void))){
        self.init(newTitle: title)
        self.alertController.addAction(UIAlertAction(title: Texts.YES, style: .default, handler: completionAccept))
        self.alertController.addAction(UIAlertAction(title: Texts.NO, style: .cancel, handler: completionRefuse))
    }

    /// call this method to set message for user
    ///
    /// - Parameter message: aler message that you want to show to user
    func message(message:String)-> AlertMessage{
        self.alertController.dismiss(animated: true, completion: nil)
        self.alertController.setValue(AlertMessage.attributed(message:message), forKey: AlertMessage.ATTRIBUTED_MESSAGE)
        return self
    }


    /// call this method to show to user the alert message
    ///
    /// - Parameter view: to show to the current view that user in
    func show(view:UIViewController){

        view.present(self.alertController, animated: true, completion: nil)



    }

    ///  call this method to show to user the alert message and handle an action
    ///
    /// - Parameters:
    ///   - view: to show to the current view that user in
    ///   - completion: action handler after message alert shown
    func show(view:UIViewController, completion: (() -> Swift.Void)? = nil){

        view.present(self.alertController, animated: true, completion: completion)


    }

and here how I initialize it in a class called "BaseViewController" that is inherited from every View controller

    /// show alert message, need in every view to alert the user about an action
    lazy var alert:AlertMessage = self.initAlertMessage()

    /// this function needed to setup the alert because the object is lazy
    ///
    /// - Returns: AlertMessage
    func initAlertMessage() -> AlertMessage{
        return AlertMessage()
    }

    /// Call this method to show alert message
    ///
    /// - Parameter message: text that user must interact with it

    func showAlartWith(message:String){

        self.alert.message(message: message).show(view: self)
    }

0 个答案:

没有答案