如何在Swift的子视图类中创建警报? [英] How to create an alert in a subview class in Swift?

查看:138
本文介绍了如何在Swift的子视图类中创建警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含子视图的视图控制器。在子视图类中,我可能需要在满足某些条件时弹出警报。

I have a view controller which contains a sub-view. And inside the sub-view class, I may need to pop out an alert when some condition is satisfied.

class GameViewController: UIViewController {
    @IBOutlet var gameBoardUIView: GameBoardUIView
    ...
}

class GameBoardUIView: UIView {
    ...
    func move() {
        if !gameBoard.checkNextMoveExist() {
            var alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Take Me Back", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
                println("Taking user back to the game without restarting")
            }))
            alert.addAction(UIAlertAction(title: "New Game", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in
                println("Starting a new game")
                self.restartGame()
            }))
            // This is where the question is
            // self.presentViewController(alert, animated: true, completion: nil)
        }
    }
}

从代码中可以看出,我无法调用presentViewController函数来显示警报,因为我的子视图不是控制器类。我应该以某种方式在子视图内创建对父控制器的周引用吗?实现此类引用的最佳做法是什么?

As you can see from the code, I cannot call presentViewController function to show the alert because my sub view is not a controller class. Should I somehow create a week reference to the parent controller inside the sub-view? What would be the best practice to implement such reference?

推荐答案

有几种方法可以捕获 UIViewController 在你的 UIView

there are couple of ways how you can catch a UIViewController in your UIView.


  1. 你可以将任何视图控制器作为委托来显示警报;

  2. 您可以将视图控制器的参考传递给您的视图;和

  3. 一般来说,您可以随时在代码中的任何位置获取 rootViewController

  1. you can make any of your view controllers as a delegate to show an alert;
  2. you can pass a view controller's reference to your view; and
  3. in general you can always grab the rootViewController anywhere in your code.

当你想要解除警报时,你需要在同一个视图控制器上调用 dismissViewControllerAnimated(_:completion :) 之后。

you need to call the dismissViewControllerAnimated(_: completion:) on the same view controller when you'd like to dismiss your alert later.

因此,我会为你的案子做一个快速的解决方案:

thus, I'd do such a quick solution for your case:

func move() {
    if !gameBoard.checkNextMoveExist() {

        let rootViewController: UIViewController = UIApplication.sharedApplication().windows[0].rootViewController

        var alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Take Me Back", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
            rootViewController.dismissViewControllerAnimated(true, completion: nil)
            println("Taking user back to the game without restarting")
        }))
        alert.addAction(UIAlertAction(title: "New Game", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in
            rootViewController.dismissViewControllerAnimated(true, completion: nil)
            println("Starting a new game")
            self.restartGame()
        }))
        rootViewController.presentViewController(alert, animated: true, completion: nil)
    }
}

这篇关于如何在Swift的子视图类中创建警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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