【问题标题】:NSObject Delegate class, pass Delegate to UIViewcontroller classNSObject Delegate 类,将 Delegate 传递给 UIViewcontroller 类
【发布时间】:2018-07-13 15:19:35
【问题描述】:

所以我有一个 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 类。

【问题讨论】:

标签: ios swift uiviewcontroller delegates nsobject


【解决方案1】:

所以这个问题的答案其实很简单,我只需要补充一下:

networkhandler.delegate = self

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多