Try this with a plain new Single View application template, so in the storyboard there is only 1 view controller
尝试使用一个普通的新单一视图应用程序模板,因此在故事板中只有一个视图控制器
In the ViewController
在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?
我们可以看到它被打印了3次,为什么呢?
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.
NSCoder调用此方法。 NSObject的实现只是返回自我。
Why is it called many times?
为什么多次打电话?
1 个解决方案
#1
0
I have tested with your suggest and I see that it call 2 times. And with this I describe my knowledge below:
我已经测试了你的建议,我看到它调用了2次。有了这个,我在下面描述我的知识:
-
ViewController
will callinitWithCoder
and after that will callawakeAfterUsingCoder
- Continue
ViewController
will callloadNibNamed
,loadNibNamed
still callawakeAfterUsingCoder
again.
ViewController将调用initWithCoder,之后将调用awakeAfterUsingCoder
继续ViewController将调用loadNibNamed,loadNibNamed仍然再次调用awakeAfterUsingCoder。
This infomation I have referenced from other source. You can view and if have some info. Please share with me. I am very appreciate :)
我从其他来源引用的这个信息。您可以查看,如果有一些信息。请与我分享。我很欣赏:)
#1
0
I have tested with your suggest and I see that it call 2 times. And with this I describe my knowledge below:
我已经测试了你的建议,我看到它调用了2次。有了这个,我在下面描述我的知识:
-
ViewController
will callinitWithCoder
and after that will callawakeAfterUsingCoder
- Continue
ViewController
will callloadNibNamed
,loadNibNamed
still callawakeAfterUsingCoder
again.
ViewController将调用initWithCoder,之后将调用awakeAfterUsingCoder
继续ViewController将调用loadNibNamed,loadNibNamed仍然再次调用awakeAfterUsingCoder。
This infomation I have referenced from other source. You can view and if have some info. Please share with me. I am very appreciate :)
我从其他来源引用的这个信息。您可以查看,如果有一些信息。请与我分享。我很欣赏:)