1

Practicing MVC pattern and want to separate view from viewcontroller using storyboard.

in main.storyboard I have a viewcontroller, and there are some uilabels in rootview.

to separate view code from viewcontroller, I selected view from viewcontroller scene , created FruitDetailView class and subclassed it in storyboard identity inspector.

enter image description here

enter image description here

And connected UILabels to FruitDetailView class.

class FruitDetailView: UIView {

    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var type: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        name.text = ""
        type.text = ""
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func update(_ detail: Fruit) {
        name.text = detail.name
        type.text = detail.type
    }
    
}

In the viewController , created FruitDetailView() instance. And in loadview() methods, assigned fruitDetailView instance to view property.

class FruitDetailViewController: UIViewController {
    
    private let fruitDetailView = FruitDetailViewController()
    var fruit = Fruit()
    
    override func loadView() {
        view = fruitDetailView
        fruitDetailView.update(fruit)
    }

}

But when I run the app, app crashes with error.

enter image description here

How can I fix this?

alphonse
  • 687
  • 1
  • 6
  • 16
  • Initialize name.text and type.text in awakeFromNib() rather than init(frame:) – Rob C Oct 26 '20 at 05:08
  • @RobertCrabtree if I initialize those properties in awakeFromNib() , no errors. but it shows only black view and I can not see any labels. – alphonse Oct 26 '20 at 05:15
  • That sounds like a completely different problem. Did you constrain your labels? What happens if you change the background color of your view? – Rob C Oct 26 '20 at 05:19
  • I set uilabel's constraints in storyboard. if I change background color to white, I see only white view. – alphonse Oct 26 '20 at 05:20
  • Are you sure the text in your textFields aren't empty strings – Rob C Oct 26 '20 at 05:22
  • I have no textFields in my view. first I want to set those labels to empty string like " ". then update when I want. – alphonse Oct 26 '20 at 05:24
  • Sorry I meant label, not textField. You aren't going to see your label if they don't have any text – Rob C Oct 26 '20 at 05:25
  • I know that. I also tried to set label's text with some string values but same result.. can see only white background – alphonse Oct 26 '20 at 05:27

1 Answers1

3

Your problem is you want to reach your FruitDetail class created before even its properties being created. Put a breakpoint to the name.text = "" and run and you will see what i mean. To solve this just control your nil value safely :

if let label = name { // by the way consider in future to assign values like nameLabel rather than name
    name.text = "put your string value"
}
DrainOpener
  • 186
  • 1
  • 18