Swift - 如何在自定义AlertController中点击按钮时显示ViewController [英] Swift - How to present ViewController when tapping button in a custom AlertController

查看:784
本文介绍了Swift - 如何在自定义AlertController中点击按钮时显示ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift 2.3中开发



我有一个Utils类,可以让我轻松创建UIAlertController。

  public class Utils {

class func buildAlertInfo(withTitle title:String?,andMessage message:String ?,withHandler处理程序:(UIAlertAction - > Void)?) - > UIAlertController {
let alertController = UIAlertController(title:title,message:message,preferredStyle:UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(标题:OK,样式:UIAlertActionStyle.Default,handler:handler ))

返回alertController
}
}

它使我能够轻松构建AlertController:

  let alert = Utils.buildAlertInfo(withTitle:Information,andMessage:约翰·斯诺正在死,withHandler:nil)
self.presentViewController(alert,animated:false,completion:nil)

我现在的问题是我想在我的Utils类中创建另一种类型的自定义警报。
例如带有按钮的警报,用户将用户导航到特定的ViewController。



我不知道如何访问自定义类中的ViewController 。也许在点击Button之后将我想要呈现的ViewController作为参数传递?



我是否应该尊重MVC模式而不与我的Utils类中的View进行交互? / p>

编辑:



我想要的提醒应如下所示:

  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(标题:OK,样式:UIAlertActionStyle.Default,handler:handler ))
alertController.addAction(UIAlertAction(标题:收藏夹,样式:UIAlertActionStyle.Default,处理程序:处理程序))

return alertController
}

OK操作相同,但收藏夹操作应导航到FavoriteViewController。

解决方案

您仍然可以使用 buildAlertInfo 函数,并且可以像这样传递处理函数。

  //在控制器中添加函数
func handler(action:UIAlertAction){
//添加当前$的代码b $ b}

现在用你的处理程序块传递这个函数

  let alert = Utils.buildAlertInfo(withTitle:Information,
andMessage:John Snow即将死去,
withHandler:self.handler)

**编辑:**对于多个操作,您可以使用您的方法创建处理程序数组。

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

alertController.addAction(UIAlertAction(标题:OK,样式:UIAlertActionStyle.Default,handler:handler.first))
alertController.addAction(UIAlertAction(标题:收藏) ,style:UIAlertActionStyle.Default,handler:handler.last))
}

// Ok处理程序
func okHandler(动作:UIAlertAction){
//添加当前代码
}

//收藏处理程序
func favoriteHandler(action:UIAlertAction){
//添加当前
的代码}

现在调用这个函数。

  let alert = Utils.buildAlertInfo(withTitle:Information,
andMessage:John Snow即将死去,
withHandler:[okHandler,favoriteHandler])


I am developing in Swift 2.3

I have an Utils class enabling me to create UIAlertController easily.

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

It enables me to build AlertController with ease :

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

My issue now is that I want to create an other type of custom Alert in my Utils Class. For example an Alert with a Button that navigates the user to a specific ViewController.

I don't know how can I access a ViewController in a Custom Class. Maybe pass as a parameter the ViewController I want to present after the Button is tapped ?

Should I respect the MVC pattern and not interact with the View in my Utils Class ?

EDIT :

The Alert I want should looks like this :

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
    }

Where the OK action is the same, but the Favorite action should navigates you to the FavoriteViewController.

解决方案

You can still use your buildAlertInfo function and can pass handler function like this way.

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

Now pass this function with your handler block

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

**Edit:**For multiple action you can create array of handler with your method like this.

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
}

Now call the function like this.

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

这篇关于Swift - 如何在自定义AlertController中点击按钮时显示ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆