簡體   English   中英

Swift-如何在自定義AlertController中點擊按鈕時顯示ViewController

[英]Swift - How to present ViewController when tapping button in a custom AlertController

我正在使用Swift 2.3開發

我有一個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類中創建其他類型的自定義警報。 例如,帶有按鈕的Alert可以將用戶導航到特定的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動作相同,但是Favorite動作應將您導航到FavoriteViewController。

關於什么

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)

您仍然可以使用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])

更加具體,並在任何項目類中使用該方法。 為此,在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)
    }

Controller中,我將調用該方法並執行所需的工作,例如:

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

注意: AlertNSObject類的名稱。

我建議使用segue標識符作為傳遞的參數(確保引用從在您調用“ buildAlert”函數的ViewController中開始的segue)。

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
}

這也可以不用創建新函數就可以實現,只需從上方將處理程序部分作為參數發送到您已有的函數即可,如下所示:

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

編輯:請注意,發送方部分可以是函數調用發生在ViewController中具有@IBOutlet引用的任何對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM