简体   繁体   English

Swift委托协议为零

[英]Swift delegate protocol is nil

I've researched this 100 times, and still can't find the answer to my problem. 我已经对此进行了100次研究,但仍然找不到我的问题的答案。 I have a very simple protocol, but it's always nil . 我有一个非常简单的协议,但始终为nil I've tried to add periodDelegate = self but get the error Cannot assign value of type 'ScoreClockPopoverViewController' to type 'PeriodDelegate!' 我尝试添加periodDelegate = self但收到错误消息: Cannot assign value of type 'ScoreClockPopoverViewController' to type 'PeriodDelegate!' I have other Protocol, using the same setup and work fine. 我有其他协议,使用相同的设置并且工作正常。

What am I missing? 我想念什么?

Thanks in advance! 提前致谢!

import UIKit

protocol PeriodDelegate {

    func changePeriodButtonImage(selectedPeriod: Period)

}

class ScoreClockPopoverViewController: UIViewController {

    //delegate
    var periodDelegate: PeriodDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()


        print("viewDidLoad / periodDelegate \(String(describing: periodDelegate!))")

    }

}

Function I need to call is in a UICollectionViewCell` 我需要调用的函数在UICollectionViewCell中

class HeaderCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        ...

    }


extension HeaderCollectionViewCell: PeriodDelegate {

    func changePeriodButtonImage(selectedPeriod: Period) {

        print("blah")

        switch selectedPeriod {
        case .first:
            print("first")
        case .second:
            print("second")
        case .third:
            print("third")
        case .overtime:
            print("overtime")
        case .shootout:
            print("shootout")

        }
    }
}

First of all, it is very uncommon to have cell as delegate for view controller. 首先,让单元格作为视图控制器的委托是非常罕见的。 Usualy, it is other way round. 通常情况是相反的。 But anyways, in your case you have to set periodDelegate as this cell, not self. 但是无论如何,在您的情况下,您必须将periodDelegate设置为此单元格,而不是self。 Cause your cell implements delegate protocol not the VC. 因为您的单元实施委托协议而不是VC。 But better rethink what do you want to do because it smells like bad design. 但是最好重新考虑您要做什么,因为它闻起来像是不良的设计。

Your statement "I have a very simple protocol, but it's always nil." 您的声明“我有一个非常简单的协议,但始终为零。” does not make sense. 没有道理。

A protocol is a specialized language. 协议是一种特殊的语言。 It can't be nil or non-nil. 它不能为nil或非nil。

Your ScoreClockPopoverViewController has a delegate property periodDelegate that conforms to the PeriodDelegate protocol, and that delegate property is nil. 您的ScoreClockPopoverViewController的委托属性periodDelegate符合PeriodDelegate协议,并且该委托属性为nil。

A delegate is a property like any other. 委托是与其他财产一样的财产。 It will be nil unless you assign a value to it. 除非您为其分配值,否则它将为nil。 It's nil because you never assigned an object as your ScoreClockPopoverViewController 's delegate. 这是零,因为您从未将对象分配为ScoreClockPopoverViewController的委托。

Who creates instances of ScoreClockPopoverViewController , and what object is supposed to be the delegate of your ScoreClockPopoverViewController ? 谁创建了ScoreClockPopoverViewController实例,以及哪个对象应该是ScoreClockPopoverViewController的委托?

Post your code that creates a ScoreClockPopoverViewController . 发布您的代码,创建一个ScoreClockPopoverViewController That's likely where you need to assign your delegate. 这可能是您需要分配代表的地方。 That code might look something like this: 该代码可能看起来像这样:

let myScoreClockPopoverViewController = storyboard.instantiateViewControllerWithIdentifier("ScoreClockPopoverViewController")
myScoreClockPopoverViewController.periodDelegate = self
present(myScoreClockPopoverViewController, 
  animated: true, 
  completion: nil)

(That code is meant as a guide and you will need to modify it to make it work in your app. You will not be able to paste it into your app without modification. ) (该代码仅供参考,您将需要对其进行修改以使其在您的应用中正常工作。 未经修改,将无法将其粘贴到您的应用中。

If you're displaying your myScoreClockPopoverViewController as a popover, as the name suggests, you'll need to adjust the code above. 顾名思义,如果将myScoreClockPopoverViewController显示为弹出窗口,则需要调整上面的代码。

I had the same problem and I fixed it following Fangming's answer by just changing 我遇到了同样的问题,并按照方命的答案进行了修改,只是进行了更改

var periodDelegate: PeriodDelegate!

to

weak var periodDelegate: PeriodDelegate? = nil

and changing the call to 并更改为

periodDelegate?.blablabla()

Swift - Error passing data between protocols / delegates (found nil) Swift-在协议/委托之间传递数据时出错(发现为零)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM