Skip to content

Instantly share code, notes, and snippets.

@robnadin
Last active August 29, 2015 14:14
Show Gist options
  • Save robnadin/e2df928cbd82af392c06 to your computer and use it in GitHub Desktop.
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
//
// 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)
}
}
//
// 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
//
// 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