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

Closed
opened 2025-10-14 15:58:46 -06:00 by navan · 0 comments
Owner

Originally created by @stefgeelen on 1/10/2020

WhatsNewKit Environment

  • WhatsNewKit version: 1.3.1
  • Xcode version: 11.2
  • Swift version: 5
  • macOS version running Xcode: Catalina
  • Dependency manager (SPM, Carthage, CocoaPods, Manually): CocoaPods

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

*Originally created by @stefgeelen on 1/10/2020* ## WhatsNewKit Environment - WhatsNewKit version: 1.3.1 - Xcode version: 11.2 - Swift version: 5 - macOS version running Xcode: Catalina - Dependency manager (SPM, Carthage, CocoaPods, Manually): CocoaPods 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
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github/WhatsNewKit#67
No description provided.