Skip to content

Instantly share code, notes, and snippets.

@sasaujp
Last active July 12, 2018 04:34
Show Gist options
  • Save sasaujp/80b2813ee82127bddfa1 to your computer and use it in GitHub Desktop.
Save sasaujp/80b2813ee82127bddfa1 to your computer and use it in GitHub Desktop.
コードからもIBからも使えるUIViewのサブクラスのinitに関して ref: https://qiita.com/sasau/items/9dd4e1c78ee669603ddb
@interface HogeView: UIView
@property(nonatomic)UIView *fugaView;
@end
@implementation HogeView
- (instancetype)initWithFrame:(CGFrame)frame {
self = [super initWithFrame:frame];
if(self) {
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
[self setup];
}
return self;
}
- (void)setup {
self.fugaView = [UIView new];
[self addSubview:self.fugaView];
}
@end
class HogeView: UIView {
let fugaView: UIView
init(_ coder: NSCoder? = nil) {
// common initialize
fugaView = UIView.init()
if let coder = coder {
super.init(coder: coder)!
} else {
super.init(frame: CGRect.zero)
}
// common setup
addSubview(fugaView)
}
required convenience init?(coder aDecoder: NSCoder) {
self.init(aDecoder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment