UIView Subclass Code Template Should Include -awakeFromNib
| Originator: | kristopherdjohnson | ||
| Number: | rdar://11064525 | Date Originated: | 16-Mar-2012 02:37 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | 4.3.1 |
| Classification: | Enhancement | Reproducible: | Not Applicable |
16-Mar-2012 02:37 PM Kristopher Johnson:
Summary: When developer creates a new UIView subclass using the "Objective-C Class" template, the generated code includes an implementation of -initWithFrame: with a place for view-initialization code to be written. This will work fine if the class is instantiated programmatically. However, if the developer places an instance of the new UIView subclass into a NIB, the developer may be surprised that the initialization code does not execute. This is due to the fact that when loaded from a NIB, a view's -init method is not called; instead its -awakeFromNib method is called.
Any UIView subclass with initialization code in its -init method will probably need identical or similar code in its -awakeFromNib. However, new iOS developers may not know this, and experienced iOS developers can easily overlook the need for -awakeFromNib. To prevent these problems, the UIView subclass template should include -awakeFromNib.
It would also be helpful to prevent the need to write the initialization code twice, so template code like this would be useful:
@implementation MyView
- (void)setup_MyView {
// initialization code to be executed by either -initWithFrame or -awakeFromNib
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup_MyView];
// other initialization code for programmatic instantiation
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setup_MyView];
// other initialization code for loading from NIB
}
Steps to Reproduce:In Xcode with a project open, choose the File > New > File.. menu item, select iOS > Cocoa Touch > Objective-C Class and click Next, enter a new class name and choose UIView from the "Subclass of" list and click Next, and click Create.
Expected Results: Generated code implements both -initWithFrame: and -awakeFromNib, and the view works fine whether generated programmatically or loaded from a NIB
Actual Results: Generated code only implements -initWithFrame, and if developer puts any non-trivial code within it, the view does not work when loaded from a NIB.
Regression:
Notes:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!