Nib IBOutlet causes app to crash
Asked Answered
O

4

7

Really need your help! I've been through every possible post addressing this issue and nothing seems to work.

So I'm using a .xib file to create a subview within ProfileFoldingCellView.swift, which works perfectly until I attempt to add an IBOutlet in the same class. Here's the code:

import Foundation
import UIKit

class ProfileFoldingCellView: UIView {

    @IBOutlet var mainLabel: UILabel!
    public var cellIndex: Int = 0

    init(index: Int) {
        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        cellIndex = index
        print("Index: \(cellIndex)")

        setupContentView()
    }

    func setupContentView() {
        let contentView = UINib(nibName: "ProfileFoldingCellView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
        contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        addSubview(contentView)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupContentView()
    }

}

As you can see, the label IBOutlet is created without any issues and there are no other outdated or unused outlets roaming around.

Label Outlet https://i.sstatic.net/5y7Ra.jpg

I've also made sure to set ProfileFoldingCellView.swift as the file owner's class.

Everything runs perfectly until I link the outlet! Then I get this error:

Error https://i.sstatic.net/ROcr4.jpg

Trust me, I've tried everything. I've probably re-created the outlet a million times, nothing is working. Any help would be greatly appreciated!

Observer answered 6/5, 2017 at 2:58 Comment(0)
L
9

Leave your File's Owner as NSObject and set your view class as ProfileFoldingCellView, your mainLabel should be connected to the UIView object and not File's Ownerenter image description here

Libelee answered 6/5, 2017 at 3:47 Comment(4)
Ok, this is definitely a step in the right direction. No error, but now the .xib UIView isn't displaying.Observer
Correction, it crashes with this error imgur.com/a/f7305. The command line just says "(lldb)"Observer
Would you have a possible solution?Observer
Man, I've been in this bug for 3 hours and you saved me. Damn it.Exemplify
O
3

I fixed it!

I simply switched the line

let contentView = UINib(nibName: "ProfileFoldingCellView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView

with the following:

let contentView = Bundle.main.loadNibNamed("ProfileFoldingCellView", owner: self, options: nil)?[0] as! UIView

I kept the file owner's class as ProfileFoldingCellView

Observer answered 7/5, 2017 at 2:47 Comment(0)
B
1

According to the image you posted, your mainLabel outlet is not connected to the label... It's connected to the files owner object which is probably your cell view and not a UILabel.

Bourbonism answered 6/5, 2017 at 3:33 Comment(3)
I tried doing that, but now the .xib UIView isn't displaying.Observer
What I would do in this case is create a nib file for the entire UITableViewCell and if you need your ProfileFoldingCellView you can build it in there. UITableView includes func register(_ nib: UINib?, forCellReuseIdentifier identifier: String) which allows you to more easily register a nib to use to create table view cells.Bourbonism
Would this be the only solution?Observer
L
0

The answer is already mentioned in the upper section. I just want to illustrate it with images so that it can be more understandable.

Step1: Do not assign the class to the file owner and leave it as NSObject enter image description here

Step2: Select your main view and Assign the class to that view enter image description here

Step3: Now connect your outletenter image description here

Now it will not cause any kind of error related to IBOutlet :)

Lacedaemonian answered 7/2, 2020 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.