Swift – 如何在自定义AlertController中点击button时呈现ViewController

我正在Swift 2.3中开发

我有一个Utils类,使我能够轻松地创buildUIAlertController。

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 } } 

它使我能够轻松构buildAlertController:

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

我现在的问题是我想在我的Utils类中创build一个其他types的自定义警报。 例如,带有将用户导航到特定ViewController的button的警报。

我不知道如何访问自定义类中的ViewController。 也许作为一个parameter passing的ViewController我想呈现button后点击?

我应该尊重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行为是相同的,但“collections”操作应该将您导航到FavoriteViewController。

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

**编辑:**对于多个动作,你可以用你的方法创build处理程序数组。

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

关于什么

 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) 

更具体一些,在任何一类项目中都使用这种方法。 为此在NSObject类中创build一个函数。 喜欢:

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

注意:这里AlertNSObject类的名字。

我build议使用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 } 

这也可以在不创build新函数的情况下实现,只需将上面的处理程序部分作为参数发送到已有的函数,如下所示:

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

编辑:请注意,发件人部分可以是在ViewController中具有@IBOutlet引用的任何对象函数调用发生