2016-04-14 43 views
-1

我嘗試創建動態警報通知,我想使用自定義的方法在一類這樣的(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開始我不明白我如何創建從全局類繼承但可定製的子類。如果有人能解釋我的好方法...

回答

0

你可以做的第一件事就是閱讀蘋果公司簡潔優雅的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()) 
    } 
}