<small id='78m8i'></small><noframes id='78m8i'>

<legend id='78m8i'><style id='78m8i'><dir id='78m8i'><q id='78m8i'></q></dir></style></legend>
    <bdo id='78m8i'></bdo><ul id='78m8i'></ul>
    <tfoot id='78m8i'></tfoot>

        <i id='78m8i'><tr id='78m8i'><dt id='78m8i'><q id='78m8i'><span id='78m8i'><b id='78m8i'><form id='78m8i'><ins id='78m8i'></ins><ul id='78m8i'></ul><sub id='78m8i'></sub></form><legend id='78m8i'></legend><bdo id='78m8i'><pre id='78m8i'><center id='78m8i'></center></pre></bdo></b><th id='78m8i'></th></span></q></dt></tr></i><div id='78m8i'><tfoot id='78m8i'></tfoot><dl id='78m8i'><fieldset id='78m8i'></fieldset></dl></div>
      1. Swift 不能通过委托调用协议方法

        时间:2023-09-12
        <i id='GiDMx'><tr id='GiDMx'><dt id='GiDMx'><q id='GiDMx'><span id='GiDMx'><b id='GiDMx'><form id='GiDMx'><ins id='GiDMx'></ins><ul id='GiDMx'></ul><sub id='GiDMx'></sub></form><legend id='GiDMx'></legend><bdo id='GiDMx'><pre id='GiDMx'><center id='GiDMx'></center></pre></bdo></b><th id='GiDMx'></th></span></q></dt></tr></i><div id='GiDMx'><tfoot id='GiDMx'></tfoot><dl id='GiDMx'><fieldset id='GiDMx'></fieldset></dl></div>
      2. <tfoot id='GiDMx'></tfoot>
          <tbody id='GiDMx'></tbody>
          <bdo id='GiDMx'></bdo><ul id='GiDMx'></ul>

        • <small id='GiDMx'></small><noframes id='GiDMx'>

              <legend id='GiDMx'><style id='GiDMx'><dir id='GiDMx'><q id='GiDMx'></q></dir></style></legend>

                • 本文介绍了Swift 不能通过委托调用协议方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两节课.一个类名为 ViewController,另一个类名为 TabView.

                  I have two classes. One class is named ViewController and the other class is named TabView.

                  我的目标是从 ViewController 调用 TabView 类中的函数 changeTab().

                  My goal is to call a function changeTab() which is inside the TabView class from the ViewController.

                  不知何故,我遇到了麻烦,因为每次我的委托都是 nil.

                  Somehow I am having trouble with it because everytime my delegate is nil.

                  这是我的 ViewController 代码:

                  protocol TabViewProtocol: class {
                      func changeTab() 
                  }
                  
                  class ViewController: NSViewController {
                      // delegate
                      weak var delegateCustom : TabViewProtocol?
                  
                      override func viewDidLoad() {
                          print(delegateCustom) // outputs "nil"
                      }
                  
                      buttonClickFunction() {
                          print(delegateCustom) // outputs "nil"
                          delegateCustom?.changeTab() // doesn't work
                      }
                  }
                  

                  这是我的 TabView 代码:

                  class TabView: NSTabViewController, TabViewProtocol {
                  
                      let myVC = ViewController()
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          myVC.delegateCustom = self
                      }
                  
                      func changeTab() {
                          print("test succeed")
                      }
                  }
                  

                  谁能解释我做错了什么?- 我是代表和协议的新手...

                  Can someone explain me what I am doing wrong? - I am new to delegates and protocols...

                  推荐答案

                  你错误地使用了委托模式.很难说您想为哪个控制器定义协议以及您想采用哪个控制器 - 但这是一种可能的方式.

                  You are using the delegate pattern wrongly. It is hard to tell which controller you want to define the protocol for and which one you want to adopt it - but here is one possible way.

                  // 1. Define your protocol in the same class file as delegate property.
                  protocol TabViewProtocol: class {
                      func changeTab() 
                  }
                  
                  // 2. Define your delegate property
                  class ViewController: NSViewController {
                      // delegate
                      weak var delegateCustom : TabViewProtocol?
                  
                      override func viewDidLoad() {
                          // It should be nil as you have not set the delegate yet.
                          print(delegateCustom) // outputs "nil"
                      }
                  
                      func buttonClickFunction() {
                          print(delegateCustom) // outputs "nil"
                          delegateCustom?.changeTab() // doesn't work
                      }
                  }
                  
                  // 3. In the class that will use the protocol add it to the class definition statement
                  
                  class TabView: NSTabViewController, TabViewProtocol {
                  
                      let myVC = ViewController()
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          myVC.delegateCustom = self
                  
                          // Should output a value now
                          print(myVC.delegateCustom) // outputs "self"
                      }
                  
                      func changeTab() {
                          print("test succeed")
                      }
                  }
                  

                  这篇关于Swift 不能通过委托调用协议方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Swift - 如何在 UITableViewCell 中使用 CollectionViewCell 打开另一个视图控制 下一篇:两个没有 segue 的 VC 之间的 Swift 委托

                  相关文章

                  <small id='TnI77'></small><noframes id='TnI77'>

                    <bdo id='TnI77'></bdo><ul id='TnI77'></ul>

                  <tfoot id='TnI77'></tfoot>
                • <i id='TnI77'><tr id='TnI77'><dt id='TnI77'><q id='TnI77'><span id='TnI77'><b id='TnI77'><form id='TnI77'><ins id='TnI77'></ins><ul id='TnI77'></ul><sub id='TnI77'></sub></form><legend id='TnI77'></legend><bdo id='TnI77'><pre id='TnI77'><center id='TnI77'></center></pre></bdo></b><th id='TnI77'></th></span></q></dt></tr></i><div id='TnI77'><tfoot id='TnI77'></tfoot><dl id='TnI77'><fieldset id='TnI77'></fieldset></dl></div>

                    1. <legend id='TnI77'><style id='TnI77'><dir id='TnI77'><q id='TnI77'></q></dir></style></legend>