Am I right in saying initWithNibName:bundle is used to manually load nib files and that initWithCoder would be used as an alternative?

折月煮酒 提交于 2019-12-05 13:10:57

NSCoding https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Protocols/NSCoding_Protocol/Reference/Reference.html

In order to understand what is going on with this method in regards to a view controller from a nib (or storyboard), you must understand NSCoding.

When objects are unarchived with NSCoding, you get a cascade effect for all objects it it owns. initWithCoder: is sent to one object, it is unfrozen, it is then sent to the objects it owns etc.

This is what the nib loading system uses to unfreeze all the objects you created in interface builder.

Here is a quick rundown of what the nib loading system does (from the docs)

  1. The nib file and referenced resources are loaded into memory
  2. The object graph created in the nib is unarchived (NSCoding) This actually depends on the type of object. UIViews are sent initWithFrame, UIViewControllers are sent initWithcoder since they conform to NSCoding and all other objects are just sent init.
  3. All outlets and action connections are established (Your IBOUtlets and IBActions) using setValue:forKey: and setTarget:action: respectively.
  4. awakeFromNib is then sent to all objects in the nib

Look here for more details under the object loading process section. https://developer.apple.com/library/ios/documentation/cocoa/conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

The point is initWithCoder will be called from your viewController when using a nib or storyboard because that is how the system unfreezes your object graph, and the properties you set on those objects in interface builder.

Also remember that a storyboard is just a collection of nib files with some metadata describing how they are related.

No worries.. We can still use -[NSViewController initWithNibName:bundle]. Are you sure that you are sub-classing your controller from NSViewController and overriding initWithNibName:bundle?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!