@IBOutlet View is nil on viewDidDisappear

321 Views Asked by At

i have problem on a custom view used in a viewController.

Assume custom xib view (AddButtonWithOptions, a button which shows some options when tapped) is being added to ViewController with interface builder.

And on the viewDidDisappear function, i am closing the options of that button. like this:

 override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        if createButton.isOn {
            createButton.switchOptionState()
        }
    }

Normally, operations work flawless, when view disappears VC is closing the options. But on crashlytics i get an error from this line of code:

 if createButton.isOn {

Report is: Crashed: com.apple.main-thread EXC_BREAKPOINT 0x00000001005592a0 SalesInvoicesHomeViewController.viewDidDisappear(_:) + 194

I can't reproduce that bug, because that button is never nil.

This is my button instance on viewController:

 @IBOutlet weak var createButton: AddButtonWithOptions!

This is how i implement setup of custom xib button:


    // Button options
    var options: [AddButtonOption] = [] {
        didSet {
            setupOptions()
        }
    }

/// Flag determines whether state is extended or reduced
    var isOn: Bool = false

 // MARK: Initialization
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        customInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        customInit()
    }

    final func customInit() {
        Bundle(for: AddButtonWithOptions.self).loadNibNamed("AddButtonWithOptions", owner: self, options: nil)
        self.addSubview(view)
        view.frame = self.bounds
    }

    // Programmatic initializer
    class func instanceFromNib() -> AddButtonWithOptions {
        return UINib(nibName: "AddButtonWithOptions", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! AddButtonWithOptions
    }

    // MARK: UI Setup: Create an add button for each option
    /// This method is called when options are passed into this component. It simply sets the state to reduced, inits option views and adds them into the options stack container, and assigns delegations
    private func setupOptions() {
        mainAddButton.delegate = self
        // Include options
        optionsContainer.isHidden = true
        optionsContainer.alpha = 0.0
        for option in options {
            let addButtonOptionView = AddButtonOptionView.instanceFromNib()
            addButtonOptionView.addButtonOption = option
            addButtonOptionView.delegate = self
            optionsContainer.addArrangedSubview(addButtonOptionView)
        }
        reduceOuterContainer()
    }

this is how i close my options:

func switchOptionState() {
        isOn = !isOn
        isOn ? extend() : reduce()
    }

I think problem happens because of ARC. As i think, It clears my xib view from memory after some time passes.

How this situation can happen?

0

There are 0 best solutions below