gpt4 book ai didi

ios - 没有调用 Swift Delegate 来关闭 UIViewController

转载 作者:行者123 更新时间:2023-11-28 13:08:18 31 4
gpt4 key购买 nike

我有一个包含游戏 Controller 的 CenterViewController。我想添加/删除用户在玩游戏时可以轻松引用的 RulesViewController。

RulesViewController 出现并被正常关闭。但是永远不会调用 delegate.continueGame 方法。我已将协议(protocol)添加到 RulesViewController。我已经向 CenterViewController 添加了一个类扩展来处理委托(delegate)。我错过了什么??非常感谢任何帮助...

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 方法中设置委托(delegate):

rulesViewController.delegate = self

:)

关于ios - 没有调用 Swift Delegate 来关闭 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105981/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com