Swift 未调用委托来关闭 UIViewController
Swift Delegate not being called to close a UIViewController
我有一个包含游戏控制器的 CenterViewController。我想要 add/remove 一个 RulesViewController,用户在玩游戏时可以轻松参考。
RulesViewController 出现并被正常关闭。但是从未调用 delegate.continueGame 方法。我已将协议添加到 RulesViewController。我已经向 CenterViewController 添加了一个 class 扩展来处理委托。我错过了什么??非常感谢任何帮助...
Class CenterViewController: UIViewController {
private var controller: GameController
required init(coder aDecoder: NSCoder){
controller = GameController()
}
override func viewDidLoad() {
// add all the views here
let gameView = UIView(frame: CGRectMake(0,0, ScreenWidth, ScreenHeight))
self.view.addSubview(gameView)
controller.gameView = gameView
}
// method called when rules button on the gameView is pressed
func showRulesForLevel () {
let rulesViewController = storyboard!.instantiateViewControllerWithIdentifier("RulesViewController") as! RulesViewController
presentViewController(rulesViewController, animated: true, completion: nil)
// extension to the Class to handle the delegate
extension CenterViewController: RulesViewControllerDelegate {
//func to continue the game
func continueGame() {
controller.gameView.userInteractionEnabled = true
}
}
在我的 RulesViewController 中:
protocol RulesViewControllerDelegate {
func continueGame()
}
class RulesViewController: UIViewController {
var delegate: RulesViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// code to add a continue button which when pressed calls continueGameAction method
}
func continueGameAction() {
// dismiss the UIViewController so game can continue
self.dismissViewControllerAnimated(true, completion: nil)
// continue the game in CenterViewController
delegate?.continueGame()
}
}
但是 delegate?.continueGame() 从未被调用过。
好的,所以您需要像这样在 showRulesForLevel
方法中设置委托:
rulesViewController.delegate = self
:)
我有一个包含游戏控制器的 CenterViewController。我想要 add/remove 一个 RulesViewController,用户在玩游戏时可以轻松参考。
RulesViewController 出现并被正常关闭。但是从未调用 delegate.continueGame 方法。我已将协议添加到 RulesViewController。我已经向 CenterViewController 添加了一个 class 扩展来处理委托。我错过了什么??非常感谢任何帮助...
Class CenterViewController: UIViewController {
private var controller: GameController
required init(coder aDecoder: NSCoder){
controller = GameController()
}
override func viewDidLoad() {
// add all the views here
let gameView = UIView(frame: CGRectMake(0,0, ScreenWidth, ScreenHeight))
self.view.addSubview(gameView)
controller.gameView = gameView
}
// method called when rules button on the gameView is pressed
func showRulesForLevel () {
let rulesViewController = storyboard!.instantiateViewControllerWithIdentifier("RulesViewController") as! RulesViewController
presentViewController(rulesViewController, animated: true, completion: nil)
// extension to the Class to handle the delegate
extension CenterViewController: RulesViewControllerDelegate {
//func to continue the game
func continueGame() {
controller.gameView.userInteractionEnabled = true
}
}
在我的 RulesViewController 中:
protocol RulesViewControllerDelegate {
func continueGame()
}
class RulesViewController: UIViewController {
var delegate: RulesViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// code to add a continue button which when pressed calls continueGameAction method
}
func continueGameAction() {
// dismiss the UIViewController so game can continue
self.dismissViewControllerAnimated(true, completion: nil)
// continue the game in CenterViewController
delegate?.continueGame()
}
}
但是 delegate?.continueGame() 从未被调用过。
好的,所以您需要像这样在 showRulesForLevel
方法中设置委托:
rulesViewController.delegate = self
:)