Swift - 如何在自定义 AlertController 中点击按钮时显示 ViewController

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

我正在开发 Swift 2.3

我有一个 Utils class 使我能够轻松创建 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 Class 中创建其他类型的自定义警报。 例如,带有将用户导航到特定 ViewController.

按钮的警报

我不知道如何在自定义 Class 中访问 ViewController。也许将 ViewController 作为参数传递我想在点击按钮后显示?

我是否应该尊重 MVC 模式而不是与我的 Utils 中的视图交互 Class?

编辑:

我想要的警报应如下所示:

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])

更具体地说,在任何 class 项目中使用该方法。为此,在 NSObject class 中创建一个函数。喜欢:

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的名称class.

我建议使用 segue 标识符作为传递的参数(确保引用从 ViewController 开始的 segue,您从中调用 "buildAlert" 函数)。

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 引用的任何对象