2017-01-11 29 views
0

我的雨燕2.3斯威夫特 - 如何在自定義按鍵敲擊時AlertController

發展我有一個utils的類使我能夠輕鬆地創建UIAlertController呈現視圖控制器。

public class Utils { 

    class func buildAlertInfo(withTitle title: String?, andMessage message: String?, withHandler handler: (UIAlertAction -> Void)?) -> UIAlertController { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler)) 

     return alertController 
    } 
} 

它讓我輕鬆地建立AlertController:

let alert = Utils.buildAlertInfo(withTitle: "Information", andMessage: "John Snow is dying", withHandler: nil) 
self.presentViewController(alert, animated: false, completion: nil) 

我現在的問題是,我想在我的utils的類來創建一個其他類型的自定義報警的。 例如帶有將用戶導航到特定ViewController的按鈕的警報。

我不知道如何訪問自定義類中的ViewController。也許作爲一個參數傳遞我想在點擊按鈕後呈現的ViewController?

我應該尊重MVC模式,而不是與我的Utils類中的View交互?

編輯:

警報我想應該是這樣的:

class func buildAlertInfoWithFavButton(withTitle title: String?, andMessage message: String?, withHandler handler: (UIAlertAction -> Void)?) -> UIAlertController { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler)) 
     alertController.addAction(UIAlertAction(title: "Favorite", style: UIAlertActionStyle.Default, handler: handler)) 

     return alertController 
    } 

凡OK的動作是一樣的,但最喜歡的動作應該導航您的FavoriteViewController。

+0

你可以使用完成處理程序去其他控制器 - withHandler而不是零通過塊,你很好 – Miknash

回答

2

你仍然可以使用你的buildAlertInfo函數,並可以像這樣傳遞處理函數。

//Add function in your controller 
func handler(action: UIAlertAction) { 
    //Add code of present 
} 

現在把這個功能與您的處理程序阻止

let alert = Utils.buildAlertInfo(withTitle: "Information", 
           andMessage: "John Snow is dying", 
           withHandler: self.handler) 

**編輯:**對於多個動作,你可以創建處理程序的數組你的方法是這樣的。

func buildAlertInfoWithFavButton(withTitle title: String?, andMessage message: String?, withHandler handler: [((UIAlertAction) -> Void)]?) -> UIAlertController { 

    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: handler.first)) 
    alertController.addAction(UIAlertAction(title: "Favorite", style: UIAlertActionStyle.Default, handler: handler.last)) 
} 

//Ok handler 
func okHandler(action: UIAlertAction) { 
    //Add code of present 
} 

//Favorite handler 
func favoriteHandler(action: UIAlertAction) { 
    //Add code of present 
} 

現在調用這個函數。

let alert = Utils.buildAlertInfo(withTitle: "Information", 
           andMessage: "John Snow is dying", 
           withHandler: [okHandler, favoriteHandler]) 
+0

如果警報具有2個按鈕,該怎麼辦?處理程序行爲會影響兩個按鈕? – Balanced

+0

@BlaBla檢查編輯的答案。 –

+0

它完美的作品。我有我的確定和最喜歡的按鈕,都將我重定向到FavoriteViewController。你的編輯修復了它。謝謝 – Balanced

2

什麼

let alert = Utils.buildAlertInfo(
    withTitle: "Information", 
    andMessage: "John Snow is dying", 
    withHandler: { action in self.go(to: specificViewController) } 
) 
self.presentViewController(alert, animated: false, completion: nil) 

+0

看到我編輯的問題。處理程序是否影響動作/按鈕? – Balanced

+0

我不明白「self.go」部分。 – Balanced

+0

'self.go'是一個你必須自己構建的函數。既然你將相同的處理函數傳遞給兩個按鈕,它將影響這兩個按鈕。由您決定如何執行該邏輯 – Guig

0

更具體和在任何類別的項目中使用該方法。爲此,在NSObject類中創建函數。像:

open class func showAlert(_ delegate: UIViewController, message: String ,strtitle: String, handler:((UIAlertAction) -> Void)! = nil) 
    { 
     let alert = UIAlertController(title: strtitle, message: message, preferredStyle: UIAlertControllerStyle.alert) 

     if handler == nil{ 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 

     } 
     else 
     { 
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: handler)) 
     } 

     delegate.present(alert, animated: true, completion: nil) 
    } 

控制器我會打電話的方法和做類似的工作要求:

Alert.showAlert(self, message: "Message", strtitle: "Tittle!!", handler: { 
         (action : UIAlertAction) in 
        //Do your Work here 
       }) 

注:這裏警報NSObject的類的名稱。

0

我建議使用segue標識符作爲傳遞的參數(確保你引用了一個從ViewController開始的segue,你稱之爲「buildAlert」函數)。

let alert = Utils.buildAlertInfo(withTitle: "Information", andMessage: "John Snow is dying", withHandler: { 
       action in 
       self.performSegue(withIdentifier: "mySegueIdentifier", sender: self) 
      }) 

編輯:

public class Utils { 

class func buildAlertInfo(withTitle title: String?, andMessage message: String?, withSegue segueIdentifier: String?, sender: Any?) -> UIAlertController { 
    let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: handler: { 
       action in 
       self.performSegue(withIdentifier: segueIdentifier, sender: sender) 
      }) 

    return alertController 
} 

這也可以在不創建一個新的功能,只需從上面發送處理部分作爲參數傳遞給你已經擁有的功能,像這樣實現注意發件人部分可以是在ViewController中有@IBOutlet引用的任何對象函數調用發生