1

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?

  • 1
    Use that code in ViewWillDisappear – Ashish Gupta Jul 16 '19 at 10:03
  • The outlet is not connected. This cannot be caused by anything else. It's a strong property, ARC doesn't have anything to do with it. – Sulthan Jul 16 '19 at 10:04
  • btw, you can use `isOn.toggle()` instead of `isOn = !isOn`. – Sulthan Jul 16 '19 at 10:05
  • @Sulthan sorry, this is the way how i tried to fix, normally, on production, it is weak reference. Now I'm editing it. That can be caused by weak reference to xib? (on my side, code is working perfectly with weak reference, i want to know the root cause) – Emir Bostancı Jul 16 '19 at 10:07
  • To be honest it seems to me that the whole problem is caused by the way you are loading your nibs. It's overcomplicated and probably not correct. Why do you even need `instanceFromNib` if you already have `init(frame:)`? – Sulthan Jul 16 '19 at 10:08
  • On another note, why do you even need to modify anything in your view on `viewDidDisapear`? – Alladinian Jul 16 '19 at 11:28
  • when i click to that button, it shows some options, and lower the opactiy of background other than options. and on disappear we want to close that options. thats why i want to close the options. @Alladinian – Emir Bostancı Jul 16 '19 at 11:48
  • Have you tried using `ViewWillDisappear` instead of `ViewDidDisapear` ? Like Ashish suggested. – Rey Bruno Jul 16 '19 at 13:47
  • @ReyBruno i can not reproduce that bug, even to test it. (still i added) i added some controls for not crashing but its not a solution.. – Emir Bostancı Jul 16 '19 at 13:53
  • Have you fixed that? I have the same crash but with ViewWillDisappear with Xamarin.iOS and I have no idea how to reproduce and fix it – Oleg Kaliuzhnyi Jan 21 '21 at 10:36
  • I fixed it by adding a null check. I think this issue is related with (garbage collection) memory clearing. Since some outlets are not used, their allocated memory is being purged. So when you try to use it, app being crashed. @Oleg – Emir Bostancı Jan 22 '21 at 11:22

0 Answers0