2016-04-14 8 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

まず、Appleのシンプルでエレガントな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()) 
    } 
} 

あなたの質問に答えるためには、これはあなたがあなたの条件を達成することができ、それを通して多くのアプローチの一つであります