【问题标题】:Swift - How to present ViewController when tapping button in a custom AlertControllerSwift - 如何在自定义 AlertController 中点击按钮时呈现 ViewController
【发布时间】:2017-01-11 10:51:41
【问题描述】:

我正在使用 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 类中创建另一种类型的自定义警报。 例如,带有 Button 的 Alert 将用户导航到特定的 ViewController。

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

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

编辑:

我想要的警报应该是这样的:

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。

【问题讨论】:

  • 您可以使用完成处理程序转到其他控制器 - withHandler 而不是 nil pass 块,您很好

标签: ios swift uialertcontroller


【解决方案1】:

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

【讨论】:

  • 如果警报有 2 个按钮怎么办?处理程序行为影响两个按钮?
  • 完美运行。我有我的确定和收藏按钮,它们都将我重定向到收藏视图控制器。您的编辑修复了它。谢谢
  • @BlaBla 欢迎朋友 :)
【解决方案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)

?

【讨论】:

  • 查看我编辑的问题。处理程序是否同时影响操作/按钮?
  • 我不明白“self.go”部分。
  • self.go 是您必须自己构建的函数。由于您将相同的处理程序传递给两个按钮,因此它将影响两个按钮。由您决定如何执行该逻辑
【解决方案3】:

更加具体,并在任何项目类别中使用该方法。为此,在 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 类的名称。

【讨论】:

    【解决方案4】:

    我建议使用 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 引用的任何对象,函数调用发生

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多