How to create an alert in a subview class in Swift?

我的未来我决定 提交于 2019-11-28 06:19:00

问题


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

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?


回答1:


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

  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.

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



回答2:


Swift 4

Just present the alert in UIApplication windows in root view controller:

let title = "Some title"
let message = "body message"

let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "Aceptar", style: .cancel, handler: nil)

alert.addAction(action)

UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)


来源:https://stackoverflow.com/questions/24584364/how-to-create-an-alert-in-a-subview-class-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!