Last active
August 29, 2015 14:14
-
-
Save robnadin/e2df928cbd82af392c06 to your computer and use it in GitHub Desktop.
How to load a UIView subclass from a nib using designated initializers in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AView.swift | |
// LoadViewFromNib | |
// | |
// Created by Rob Nadin on 27/01/2015. | |
// Copyright (c) 2015 Rob Nadin. All rights reserved. | |
// | |
import UIKit | |
class AView: NibView { | |
override convenience init() { | |
self.init(frame: .zeroRect) | |
} | |
override init(frame: CGRect) { | |
super.init(nib: UINib(nibName: "AView", bundle: nil)) | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NibView.h | |
// LoadViewFromNib | |
// | |
// Created by Rob Nadin on 27/01/2015. | |
// Copyright (c) 2015 Rob Nadin. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface NibView : UIView | |
- (instancetype)initWithNib:(UINib *)nib; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NibView.m | |
// LoadViewFromNib | |
// | |
// Created by Rob Nadin on 27/01/2015. | |
// Copyright (c) 2015 Rob Nadin. All rights reserved. | |
// | |
#import "NibView.h" | |
@implementation NibView | |
- (instancetype)initWithNib:(UINib *)nib | |
{ | |
if (!nib) nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; | |
self = [[nib instantiateWithOwner:nil options:nil] firstObject]; | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment