<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 不能通过委托调用协议方法

        Swift can#39;t call protocol method via delegate(Swift 不能通过委托调用协议方法)
        <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 不能通过委托调用协议方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员

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

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

                    <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>