迅速动态UIAlertController

问题描述:

我尝试创建动态警报通知,我想使用自定义的方法在一类这样的(SETERROR:红色背景,setSuccess:绿色背景,...):迅速动态UIAlertController

AlertHelpers.SetError("Error Password", viewController: self) 

这是我的代码,我不知道它的好办法:

class AlertHelpers { 

    func notificationAlert(message:String, viewController : UIViewController) { 

     //Create alert Controller _> title, message, style 
     let alertController = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 
     //Create button Action -> title, style, action 
     let successAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
      UIAlertAction in 
      NSLog("OK Pressed") 
     } 

     alertController.addAction(successAction) 

     viewController.presentViewController(alertController, animated: true, completion:nil) 
    } 
} 

class SetError : AlertHelpers { 
    override func notificationAlert(message:String, viewController : UIViewController) {} 
    alertController.view.backgroundColor = .redColor() //RED BACKGROUND 
} 

alertController不承认,我无法找到解决通过的消息,查看通过SETERROR。

我从POO开始我不明白我如何创建从全局类继承但可定制的子类。如果有人能解释我的好方法...

你可以做的第一件事就是阅读苹果公司简洁优雅的Swift文档here

要回答你的问题,这是众多方法之一,通过它可以实现您的要求:

class AlertHelper { 

    private class func notificationAlert(message:String, viewController : UIViewController, color: UIColor) { 

     //Create alert Controller _> title, message, style 
     let alertController = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 

     // set the background color here 
     alertController.view.backgroundColor = color 

     //Create button Action -> title, style, action 
     let successAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
      UIAlertAction in 
      NSLog("OK Pressed") 
     } 

     alertController.addAction(successAction) 

     viewController.presentViewController(alertController, animated: true, completion:nil) 
    } 

    class func displayError(message:String, viewController : UIViewController) { 

     notificationAlert(message, viewController: viewController, color: .redColor()) 
    } 

    class func displaySuccess(message:String, viewController : UIViewController) { 

     notificationAlert(message, viewController: viewController, color: .greenColor()) 
    } 
}