0

Try this with a plain new Single View application template, so in the storyboard there is only 1 view controller

In the ViewController

class ViewController: UIViewController {
    override func awakeAfterUsingCoder(aDecoder: NSCoder) -> AnyObject? {
        print(self)
        return self
    }
}

We can see that it is printed 3 times, why is that?

In the spec

Overridden by subclasses to substitute another object in place of the object that was decoded and subsequently received this message.

You can use this method to eliminate redundant objects created by the coder. For example, if after decoding an object you discover that an equivalent object already exists, you can return the existing object. If a replacement is returned, your overriding method is responsible for releasing the receiver.

This method is invoked by NSCoder. NSObject’s implementation simply returns self.

Why is it called many times?

Community
  • 1
  • 1
onmyway133
  • 45,645
  • 31
  • 257
  • 263

1 Answers1

0

I have tested with your suggest and I see that it call 2 times. And with this I describe my knowledge below:

  • ViewController will call initWithCoder and after that will call awakeAfterUsingCoder
  • Continue ViewController will call loadNibNamed, loadNibNamed still call awakeAfterUsingCoder again.

This infomation I have referenced from other source. You can view and if have some info. Please share with me. I am very appreciate :)

Community
  • 1
  • 1
vien vu
  • 4,277
  • 2
  • 17
  • 30