简体   繁体   中英

Swift delegate protocol is nil

I've researched this 100 times, and still can't find the answer to my problem. I have a very simple protocol, but it's always nil . I've tried to add periodDelegate = self but get the error 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`

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. Cause your cell implements delegate protocol not the 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.

Your ScoreClockPopoverViewController has a delegate property periodDelegate that conforms to the PeriodDelegate protocol, and that delegate property is nil.

A delegate is a property like any other. It will be nil unless you assign a value to it. It's nil because you never assigned an object as your ScoreClockPopoverViewController 's delegate.

Who creates instances of ScoreClockPopoverViewController , and what object is supposed to be the delegate of your ScoreClockPopoverViewController ?

Post your code that creates a 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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