簡體   English   中英

沒有調用Swift Delegate關閉UIViewController

[英]Swift Delegate not being called to close a UIViewController

我有一個包含游戲控制器的CenterViewController。 我想添加/刪除一個RulesViewController,用戶在玩游戲時可以輕松參考。

出現RulesViewController並被罰款。 但是從來沒有調用委托.continueGame方法。 我已將協議添加到RulesViewController。 我向CenterViewController添加了一個類擴展來處理委托。 我想念什么? 任何幫助表示贊賞...

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

但是永遠不會調用委托?.continueGame()。

好的,因此您需要在showRulesForLevel方法中設置委托,如下所示:

rulesViewController.delegate = self

:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM