代表队伍快速通过 [英] delegate passing nil in swift
问题描述
我正在试图抓住代表,但是我设置的代表似乎是零,我不知道为什么。我有一个 HomeViewController
游戏从哪里开始,然后是一个 UITableViewController
,播放器从表中选择一行。然后使用行索引来拉取要在游戏中使用的数据。 UITableViewController
返回到游戏开始的 HomeViewController
。我以为我把正确的协议和代码代码放在一起,但是$ 委托
似乎是 nil
。
任何帮助非常感谢!
I am trying to get to grips with delegates but the delegate I have set up seems to be nil and I am not sure why. I have a HomeViewController
where the game is started from, then a UITableViewController
where the player selects a row from a table. The row index is then used to pull data to be used in the game. The UITableViewController
segues back to the HomeViewController
where the game then starts. I thought I had put the correct protocol and delegate code in place but the delegate
seems to be nil
.
Any help much appreciated!
import UIKit
import Foundation
class HomeViewController: UIViewController, WordListsTableViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// sets up the game here
}
func wordListSelected(selectedWordList: Int) {
// passes the index path of the table to the AppWordList class to create the wordList for the game.
controller.wordList = AppWordList(wordListNumber: selectedWordList)
}
和TableViewController
and in the TableViewController
import UIKit
protocol WordListsTableViewControllerDelegate {
func wordListSelected(selectedWordList: Int)
}
class WordListsTableViewController: UITableViewController {
var delegate: WordListsTableViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
reloadData()
tableView.reloadData()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedWordList = Int()
if (indexPath.section) == 2 {
selectedWordList = (indexPath.row) // Console shows the row is being selected ok.
delegate?.wordListSelected(selectedWordList) // IS NIL ???
// exit segue back to the HomeVC
performSegueWithIdentifier("startGameSegue", sender: nil)
}
}
推荐答案
你不太了解代表模式需要保留对委托其函数的类的引用,并在处理该函数的类中设置它 delegate
。因此,假设您从 HomeViewController
中的 c> WordListsTableViewController 中显示,如下例所示:
You're missing a very important point about the Delegate Pattern, you need to keep a reference to the class that delegate its function and set it delegate
in the class that handle the function. So let suppose you present the WordListsTableViewController
by a segue from the HomeViewController
like in the following example:
class HomeViewController: UIViewController, WordListsTableViewControllerDelegate {
// the reference to the class that delegate
var wordListTableViewController: WordListsTableViewController!
override func viewDidLoad() {
super.viewDidLoad()
// sets up the game here
}
func wordListSelected(selectedWordList: Int) {
// passes the index path of the table to the AppWordList class to create the wordList for the game.
controller.wordList = AppWordList(wordListNumber: selectedWordList)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// get the reference to the WordListsTableViewController
self.wordListTableViewController = segue.destinationViewController as! WordListsTableViewController
// set as it delegate
self.wordListTableViewController.delegate = self
}
}
然后,您应该从 WordListsTableViewController
中收到通知,在上述示例中我假设使用segues,但是如果您提供 WordListsTableViewController
,您可以使用相同的原则来保留对代理类的引用,就像我在上面的示例中所示。
And then you should be notified from the WordListsTableViewController
, in the above example I assume the use of segues, but if you present the WordListsTableViewController
you can use the same principle of keep a reference to the delegate class, like I show in the above example.
我不会在代码中对使用代理可能发生的保留周期的代码中应用任何概念,但是您可以在我关于如何正确实现代理的这个问题的答案中阅读更多内容:
I do not apply any concept in code regarding the retain-cycles that can be happen in the use of delegates, but you can read more in my answer of this question about how to implement delegates correctly:
- "fatal error: unexpectedly found nil while unwrapping an Optional value" while calling a protocol method
我强烈建议您阅读有关代理模式的更多信息这篇文章:
I strongly recommend you read more about the Delegate Pattern in this post:
- How Delegation Works – A Swift Developer’s Guide
我希望这可以帮助你。
这篇关于代表队伍快速通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!