When to use viewDidLoad and when to use awakeFromN

2019-03-27 12:50发布

I've gotten pretty comfortable using the viewDidLoad method to execute things I want done at the beginning of a view, but reading one of Apple's tutorials they set the data controller for the class in the awakeFromNib method and did nothing in the awakeFromNib. I swapped it and it seemingly worked identically in my app, but I'm not sure if it was better to have it in awakeFromNib or viewDidLoad.

When should I use either one?

标签: ios xcode ios6
2条回答
Summer. ? 凉城
2楼-- · 2019-03-27 12:56

awakeFromNib is called when the associated nib file with a class is loaded . Any class that can own a nib can use it. viewDidLoad is used only by view controllers. It is usually called when loading from nib as well but it can also be called by a view created in memory (very rare circumstance.). If you are using controllers, then I would suggest you to use viewDidLoad

For more Refer this Answer

查看更多
甜甜的少女心
3楼-- · 2019-03-27 13:07

viewDidLoad is associated with the view controller. If you need to initialize another control unarchived from the nib (e.g. UITableViewCell prototype) you cannot overload viewDidLoad, you need to overload awakeFromNib.

查看更多
登录 后发表回答