2017-12-12 3 views
0

私のアプリケーションには複数のView Controllerがあります。それぞれの状況では、いくつかの条件に基づいてアラートを表示する必要があります。それぞれに警告コントローラを追加するのではなく、継承を次のように使ってみました。UIViewControllerと継承

UIExtension.swift

class UIExtension: UIViewController { 

    func prepareAlert(title: String, message: String) -> UIAlertController { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) 
     return alert 
    } 

} 

FirstViewController.swiftアラートを表示する他のviewcontrollersにUIExtension用いる同様

class FirstViewController: UIExtension { 

    //somewhere inside used the following 
    present(prepareAlert(title: "Error Validation", message: "invalid fields"), animated: true, completion: nil) 

} 

、。この方法はお勧めですか?

答えて

2

prepareAlertメソッドをUIViewControllerエクステンションに追加する方がよいでしょう。サブクラス化は必要ありません。

extension UIViewController { 
    func prepareAlert(title: String, message: String) -> UIAlertController { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) 
     return alert 
    } 
} 

は、その後、あなたのビューコントローラは:

class FirstViewController: UIViewController { 
    //somewhere inside used the following 
    present(prepareAlert(title: "Error Validation", message: "invalid fields"), animated: true, completion: nil) 
} 

これは、あなたが任意のビューコントローラからprepareAlertを使用することができます含まUITableViewControllerUICollectionViewControllerなど

1

あなたが考える場合がアプローチは、技術的に正しいです条件に関係なくすべてUIViewControllerのインスタンスを拡張すると、それを直接拡張する方が便利です。

extension UIViewController { 
    func prepareAlert(title: String, message: String) -> UIAlertController { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) 
     return alert 
    } 
} 

rmaddyが高速でした。しかし、私は答えを削除しないことにしましたが、別のアイデアを追加しました。

もう1つのアプローチは、プロトコルを特定の機能のためのラッパーとして使用することです。これも広く使用されています。

言ってやるが、あなたがこのような場合には、アラートを生成するように、いくつかの機能に関連するプロトコル、持っている:あなたは、特定のUIViewControllerインスタンスいつでも、次に

protocol Alertable {} // or whatever else name 

extension Alertable { 
    func prepareAlert(title: String, message: String) -> UIAlertController { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) 
     return alert 
    } 
} 

(または任意の他のクラスは、あなたのアイデアを得ます)この機能に関連していることが、単に実行します。

class FirstViewController: UIViewController, Alertable { 
    // Now you can do the same: 

    present(prepareAlert(title: "Error Validation", message: "invalid fields"), animated: true, completion: nil) 

} 

はそれで特定のクラスを関連付ける、プロトコルを構成すると、それを拡張し、そして、要約すると - その機能を公開することは - 非常に便利で有用な練習です。特に、これは、グローバル/クラス全体のアクセスを意味しない場合など、いくつかの機能をカプセル化するための良い方法です。

1

私はそれはあなたがUIViewControllerクラスの任意の場所でそれを使用して:)

extension UIViewController { 
    let kAPPNAME = "Your App name" 

    func showOkAlert(_ msg: String) { 
     let alert = UIAlertController(title: 
      kAPPNAME, message: msg, preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) 
     alert.addAction(okAction) 
     present(alert, animated: true, completion: nil) 
    } 

    func showOkAlertWithHandler(_ msg: String,handler: @escaping()->Void){ 
     let alert = UIAlertController(title: kAPPNAME, message: msg, preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "OK", style: .default) { (type) -> Void in 
      handler() 
     } 
     alert.addAction(okAction) 
     present(alert, animated: true, completion: nil) 
    } 

    func showAlertWithActions(_ msg: String,titles:[String], handler:@escaping (_ clickedIndex: Int) -> Void) { 
     let alert = UIAlertController(title: kAPPNAME, message: msg, preferredStyle: .alert) 
     for title in titles { 
      let action = UIAlertAction(title: title, style: .default, handler: { (alertAction) in 
       //Call back fall when user clicked 
       let index = titles.index(of: alertAction.title!) 
       if index != nil { 
        handler(index!+1) 
       } 
       else { 
        handler(0) 
       } 
      }) 
      alert.addAction(action) 
     } 
     present(alert, animated: true, completion: nil) 
    } 

    func showOkCancelAlertWithAction(_ msg: String, handler:@escaping (_ isOkAction: Bool) -> Void) { 
     let alert = UIAlertController(title: kAPPNAME, message: msg, preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "OK", style: .default) { (action) -> Void in 
      return handler(true) 
     } 
     let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in 
      return handler(false) 
     } 
     alert.addAction(cancelAction) 
     alert.addAction(okAction) 
     present(alert, animated: true, completion: nil) 
    } 
} 

class FirstViewController: UIViewController { 
     override func viewDidLoad() { 
     super.viewDidLoad() 

     //Only Info 
     self.showOkAlert("Hello") 

     //Info with Okay button 
     self.showOkAlertWithHandler("Hello Again") { 
      print("Tap to Okay") 
     } 

     //Show alert with Okay and cancel 
     self.showOkCancelAlertWithAction("Hello with Cancel") { (isOk) in 
      if isOk { 
       print("Okay") 
      } 
      else { 
       print("Cancel") 
      } 
     } 

     //Show alert with actions   
     self.showAlertWithActions("Hello with action", titles: ["Allow","Don't Allow", "Cancel"]) { (tapIndex) in 
      if tapIndex == 1 { 
       print("Allow") 
      } 
     } 
    } 
} 
を使用していますを楽しむことができ、ほとんどのアプリケーションで頻繁に使用されている、あなたと共有していますいくつかの拡張メソッド