SvenTiigi / WhatsNewKit

Showcase your awesome new app features 📱
https://sventiigi.github.io/WhatsNewKit/
MIT License
3.87k stars 190 forks source link

Custom action for completion button has no reference to Self #36

Closed stefgeelen closed 4 years ago

stefgeelen commented 4 years ago

WhatsNewKit Environment

Hi guys,

I have the current implementation of WhatsNewKit

import Foundation
import WhatsNewKit

@objc protocol SMSCWhatsNewKitWrapperDelegate: class {
    func whatsNewDidFinish(controller: UIViewController)
}

@objc class SMSCWhatsNewKitWrapper: NSObject {
    var account: SMSCAccount
    @objc weak var delegate: SMSCWhatsNewKitWrapperDelegate?

   @objc init(account: SMSCAccount) {
        self.account = account

        super.init()
    }

    @objc func getWhatsNewViewcontroller() -> UIViewController? {
        let items = createWhatsNewItems()
        let filteredItems = items.filter { $0.appliesToAccount(account: account) }.map { $0.item }
        let whatsNew = WhatsNew(title: "Title", items: filteredItems)
        let configuration = createConfiguration()

        let keyValueVersionStore = KeyValueWhatsNewVersionStore(
            keyValueable: UserDefaults.standard
        )

        return WhatsNewViewController(whatsNew: whatsNew,
                                      configuration: configuration,
                                      versionStore: InMemoryWhatsNewVersionStore())
    }

    private func createWhatsNewItems() -> [WhatsNewItem] {
        var items: [WhatsNewItem] = []

        //Add items

        return items
    }

    private func createConfiguration() -> WhatsNewViewController.Configuration {
        var configuration = WhatsNewViewController.Configuration(theme: .default,
                                                                 backgroundColor: .white,
                                                                 completionButton: .init(title: "Continue",
                                                                                         action: .custom(action: { [weak self] whatsNewViewController in
                                                                                                                        print("LOG 5: in custom action")
                                                                                                                        self?.delegate?.whatsNewDidFinish(controller: whatsNewViewController)
                                                                                         })))
        configuration.backgroundColor = .white
        configuration.titleView.titleColor = .black
        configuration.completionButton.backgroundColor = .red
        configuration.itemsView.contentMode = .center
        configuration.apply(animation: .slideUp)
    }
}

I would expect that when I press my completion button my delegate is being triggered. After debugging I found out that in my completion block of the completionButton the property self is nil.

Any help? Thanks in advance!

Kind regards, Stef

SvenTiigi commented 4 years ago

Hey @stefgeelen,

It seems like your SMSCWhatsNewKitWrapper gets deallocated too early. This explains why self which is referencing your SMSCWhatsNewKitWrapper instance is nil.

Please check where and how you are storing (weak, strong) the instance of the SMSCWhatsNewKitWrapper class and maybe add a deinit in order to add a breakpoint to debug in which moment of the runtime your SMSCWhatsNewKitWrapper class get deallocated.

But as far as I checked your code this is not an issue of WhatsNewKit as the completion closure gets correctly called when the completion button has been tapped.

stefgeelen commented 4 years ago

Hi Sven,

You were right, the problem was the deallocation. This can be closed. Thanks for the help.