Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is "required init?(coder aDecoder: NSCoder)" called on a UIView or UIViewController?

When I create a subclass of UIView or UIViewController with a stored property, Xcode will not compile my project unless I include an implementation of required init?(coder aDecoder: NSCoder). Currently, I have the following implementation to shut the compiler up:

required init?(coder aDecoder: NSCoder) {
    fatalError()
}

I understand why I'm required to include this initializer; my subclass needs to conform to the NSCoding protocol because its superclass conforms to it, and this initializer is part of the NSCoding protocol so it needs to work with my class, i.e. initialize all of my class's stored properties (which the superclass version of the initializer won't do).

I imagine that a correct implementation would look something like this:

class MyView: UIView {
    let label: UILabel

    override init(frame: CGRect) {
        label = UILabel()
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        if let label = aDecoder.decodeObject() as? UILabel {
            self.label = label
        } else {
            return nil
        }
        super.init(coder: aDecoder)
    }

    override func encode(with aCoder: NSCoder) {
        aCoder.encode(label)
        super.encode(with: aCoder)
    }
}

However, considering that my application has over 50 custom views and view controllers, correctly implementing this function in every custom view and view controller is a lot of work.

So, I'm wondering if it's necessary to implement this initializer correctly, or if I can just leave it throwing a fatal error. In other words, will this initializer ever be called if I don't call it in my own code? I think I read that it might be called by a Storyboard, but my app doesn't use any Storyboards.

like image 822
kmell96 Avatar asked Sep 08 '18 21:09

kmell96


People also ask

What is a UIViewController?

A view controller may present a different view controller to display a new set of views, or it may act as a container for other view controllers’ content and animate views however it wants. Every app contains at least one custom subclass of UIViewController.

What are the options for transitions between UIView controllers?

func transition(from: UIViewController, to: UIViewController, duration: TimeInterval, options: UIView.AnimationOptions, animations: ( () -> Void)?, completion: ( (Bool) -> Void)?) Transitions between two of the view controller's child view controllers.

How many UIView Controllers does an app have?

Every app contains at least one custom subclass of UIViewController. More often, apps contain many custom view controllers. Custom view controllers define the overall behaviors of your app, including the app’s appearance and how it responds to user interactions.

What is UIViewController didreceivememorywarning () method?

The UIViewController class provides some automatic handling of low-memory conditions through its didReceiveMemoryWarning () method, which releases unneeded memory. If you assign a value to the view controller's restorationIdentifier property, the system may ask the view controller to encode itself when the app transitions to the background.


1 Answers

This initialiser will be called if an instance of your view is used in a storyboard scene.

It is up to you whether to create a functioning initialiser or not, but it should mostly be a matter of copying code from init(frame:)

like image 54
Paulw11 Avatar answered Oct 22 '22 07:10

Paulw11