2016-04-14 29 views
-1

hızlı dinamik UIAlertController, ben bu (: kırmızı arka plan, setSuccess: yeşil arka plan, ... setError) gibi bir sınıfta özel yöntemini kullanmak istiyorumDinamik uyarı bildirimlerini oluşturmayı deneyin

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 tanımaz ve ben ileti gönderip setError yoluyla görüntülemek için çözüm bulamıyor: iyi yolu ise benim kod, bilmiyorum.

POO'da başladım Küresel sınıftan miras alınan ancak özelleştirilebilir alt sınıfları nasıl oluşturabileceğimi anlamıyorum. Birisi bana iyi yolu açıklayabilir ...

cevap

0

Yapabileceğiniz ilk şey, Apple'ın basit ve zarif Swift belgeleri 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()) 
    } 
} 
:

sorunuza gelince, bu size ihtiyacı elde edebilirsiniz hangi aracılığıyla birçok yaklaşımlardan biridir

İlgili konular