gpt4 book ai didi

ios - NSObject Delegate类,将Delegate传递给UIViewcontroller类

转载 作者:行者123 更新时间:2023-11-30 11:48:10 25 4
gpt4 key购买 nike

所以我有一个 NSObject 类型的网络 Controller 类,并且在其中我有一个委托(delegate)协议(protocol):

protocol NetworkControllerDelegate: class {
func stateChanged(_ state: NetworkState)
func setNotInMatch()
func player(_ playerIndex: UInt8, movedToPosX posX: Int)
func matchStarted(_ match: Match)
}

我有一个 UIViewController 类型的 ViewController,它尝试接收委托(delegate)更新,但它不起作用。在我的网络 Controller 中我设置:

var delegate: NetworkControllerDelegate?

我还触发了委托(delegate)函数

delegate?.matchStarted(match!)

在 UIViewController 中,我遵守委托(delegate)并监听,但没有任何反应

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() 只是调用网络 Controller 中的一个函数,最终触发委托(delegate),委托(delegate)被触发,但我的 UIViewController 中没有收到它,我在网上阅读过,但它们似乎都来自UIViewController 到 UIViewController。不过,我不是在 View 之间导航,而是调用应该触发委托(delegate)的 NSObject 类。

最佳答案

所以这个问题的答案实际上很简单,我只需添加:

networkhandler.delegate = self

  override func viewDidLoad() {
super.viewDidLoad()
networkhandler.delegate = self
// Do any additional setup after loading the view.
}

关于ios - NSObject Delegate类,将Delegate传递给UIViewcontroller类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594268/

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