所以我有一个NSObject类型的网络控制器类,并且我有一个委托协议:
protocol NetworkControllerDelegate: class {
func stateChanged(_ state: NetworkState)
func setNotInMatch()
func player(_ playerIndex: UInt8, movedToPosX posX: Int)
func matchStarted(_ match: Match)
}
我有一个UIViewController类型的ViewController,试图接收委托更新,但它不起作用。在我的网络控制器中,我设置了:
var delegate: NetworkControllerDelegate?
我还会触发委托功能
delegate?.matchStarted(match!)
在UIViewController中的我遵守委托并听取但没有任何反应
class ConnectPlayerViewController: UIViewController, NetworkControllerDelegate {
func stateChanged(_ state: NetworkState) {
print("stateChanged")
}
func setNotInMatch() {
print("sentNotInMatch")
}
func player(_ playerIndex: UInt8, movedToPosX posX: Int) {
print("recieved Player Update")
}
let networkhandler = NetWorkController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func ConnectUser(_ sender: UIButton) {
networkhandler.connect()
}
func matchStarted(_ match: Match) {
print(match)
}
}
networkhandler.connect()只调用网络控制器中的一个函数,该函数最终触发委托,代理被触发但是我的UIViewController没有收到它,我在线阅读但它们似乎都来自UIViewController到UIViewController。但是我不是在视图之间导航,而是调用应该触发委托的NSObject类。
答案 0 :(得分:0)
所以这个问题的答案实际上非常简单,我只需添加:
networkhandler.delegate = self
in
override func viewDidLoad() {
super.viewDidLoad()
networkhandler.delegate = self
// Do any additional setup after loading the view.
}