Home > OS >  Property 'self.navigation' not initialized at super.init call
Property 'self.navigation' not initialized at super.init call

Time:07-26

How do I open a ViewController from @IBAction. In below @IBAction I tried to open a ViewController but I am getting Property 'self.navigation' not initialized at super.init call error:

 // MARK: - IBActions
    @IBAction func doAddCard() {
        // OTHER CODE GOES HERE
        
        let viewController = PaymentsStoryboard.confirmKyc()
        navigation.pushViewController(viewController, animated: true)
        
       //...    
}

Full Code:

import UIKit

final class ManualCardAddView: UIView, XibInitializable, DialogKeyboardDelegate {
    
    var completionHandler: ((CreditCard, String) -> Void)?
    private unowned let navigation: UINavigationController
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    private func setup() {
        loadFromXib()
        
        // ...
    }
    
    @IBAction func doAddCard() {
        
        let viewController = PaymentsStoryboard.confirmKyc()
        navigation.pushViewController(viewController, animated: true)
        
        // ...
    }
}

How do I resolve above issue?

Thank You!

CodePudding user response:

You need to change navigation to a mutable optional property, since it only gets set after the initialisation. All immutable properties (and mutable non-optional ones as well) must be set during initialisation.

So change

private unowned let navigation: UINavigationController

to

private unowned var navigation: UINavigationController?
  • Related