Swift - 如何将视图控制器的引用传递给子 UIView 类? [英] Swift - How to pass a view controller's reference to a sub UIView class?

查看:24
本文介绍了Swift - 如何将视图控制器的引用传递给子 UIView 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在里面有一个 UIViewController 和一个 UIView.当我尝试在 UIView 中添加警报时,我必须使用控制器来呈现 UIAlertController.如何将 UIViewController 的引用传递给 UIView 类?或者,我如何创建控制器的委托?

I have a UIViewController and a UIView inside it. When I try to add an alert inside the UIView, I have to use the controller to present the UIAlertController. How do I pass the reference of UIViewController to the UIView class? Or alternatively how do I create a delegate of controller?

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: "Ok", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
                println("Game Over")
            }))
            }))
            // Following would fail because self is not a UIViewController here
            // self.presentViewController(alert, animated: true, completion: nil)
        }
    }
}

推荐答案

遵循 MVC 模式,ViewController 知道它的 Views,但 View 不应该知道 ViewController.相反,您应该为您的 ViewController 采用的 GameBoardUIView 声明委托协议,如下所示:

Following the MVC pattern, a ViewController knows about its Views, but the View shouldn't know about the ViewController. Instead you should declare delegate protocol for GameBoardUIView that your ViewController adopts as follows:

// Delegate protocol declared here
protocol GameBoardUIViewDelegate: class {
    func checkIfNextMoveExistsForGameBoardUIView(gameBoardUIView: GameBoardUIView)
}

class GameBoardUIView: UIView {

    // GameBoardUIView has a delegate property that conforms to the protocol
    // weak to prevent retain cycles
    weak var delegate:GameBoardUIViewDelegate?

    func move() {
        if !gameBoard.checkNextMoveExist() {
            delegate?.checkIfNextMoveExistsForGameBoardUIView(gameBoardUIView: self)
        }
    }

}

// View controller subclass adopts the protocol
class GameViewController: UIViewController, GameBoardUIViewDelegate {

    @IBOutlet var gameBoardUIView: GameBoardUIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        gameBoardUIView.delegate = self
    }

    // Delegte protocol method
    func checkIfNextMoveExistsForGameBoardUIView(gameBoardUIView: GameBoardUIView) {

        let alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: {(action: UIAlertAction!) in
            print("Game Over")
        }))

        // If you need to feed back to the game view you can do it in the completion block here
        present(alert, animated: true, completion: nil)
    }
}

这篇关于Swift - 如何将视图控制器的引用传递给子 UIView 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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