Skip to content

Instantly share code, notes, and snippets.

@lukaskubanek
Created June 17, 2014 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskubanek/159c2df832dcee6aec03 to your computer and use it in GitHub Desktop.
Save lukaskubanek/159c2df832dcee6aec03 to your computer and use it in GitHub Desktop.
Replacing the class to subclass after loading a custom view from Interface Builder
//
// CustomView.h
// IBSubclassTest
//
// Created by Lukas Kubanek on 17.06.2014.
// Copyright (c) 2014 Lukas Kubanek. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CustomView : UIView
@end
//
// CustomView.m
// IBSubclassTest
//
// Created by Lukas Kubanek on 17.06.2014.
// Copyright (c) 2014 Lukas Kubanek. All rights reserved.
//
#import "CustomView.h"
#import "SubclassCustomView.h"
@implementation CustomView
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
Class subclass = [SubclassCustomView class];
return [[subclass alloc] initWithCoder:aDecoder];
}
@end
//
// SubclassCustomView.h
// IBSubclassTest
//
// Created by Lukas Kubanek on 17.06.2014.
// Copyright (c) 2014 Lukas Kubanek. All rights reserved.
//
#import "CustomView.h"
@interface SubclassCustomView : CustomView
@end
//
// SubclassCustomView.m
// IBSubclassTest
//
// Created by Lukas Kubanek on 17.06.2014.
// Copyright (c) 2014 Lukas Kubanek. All rights reserved.
//
#import "SubclassCustomView.h"
@implementation SubclassCustomView
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.backgroundColor = [UIColor blueColor];
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment