I have an Objective-C UIView
class:
ChoosePersonView.h
@class Person;
@interface ChoosePersonView : MDCSwipeToChooseView
@property (nonatomic, strong, readonly) Person *person;
- (instancetype)initWithFrame:(CGRect)frame
person:(Person *)person
options:(MDCSwipeToChooseViewOptions *)options;
@end
MDCSwipeToChooseView.m
@class MDCSwipeToChooseViewOptions;
@interface MDCSwipeToChooseView : UIView
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *likedView;
@property (nonatomic, strong) UIView *nopeView;
- (instancetype)initWithFrame:(CGRect)frame
options:(MDCSwipeToChooseViewOptions *)options;
@end
Normally I would subclass and use it like the following:
ChoosePersonView.m
@implementation ChoosePersonView
#pragma mark - Object Lifecycle
- (instancetype)initWithFrame:(CGRect)frame
person:(Person *)person
options:(MDCSwipeToChooseViewOptions *)options {
self = [super initWithFrame:frame options:options];
if (self) {
_person = person;
self.imageView.image = _person.image;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleBottomMargin;
self.imageView.autoresizingMask = self.autoresizingMask;
[self constructInformationView];
}
return self;
}
...
@end
The problem I'm facing is when I try to do this in Swift, the property self.imageView
is always nil
.
I'm not sure how to re-implement initWithFrame
as it's implemented in Objective-C within Swift. I thought using init(frame: CGRect, person: Person, options: MDCSwipeToChooseViewOptions)
was the correct way but I think I may be wrong.
ChoosePersonView.swift
class ChoosePersonView: MDCSwipeToChooseView {
var informationView: UIView!
var nameLabel: UILabel!
var cameraImageLabelView: ImageLabelView!
var interestsImageLabelView: ImageLabelView!
var friendsImageLabelView: ImageLabelView!
var person: Person!
let ChoosePersonViewImageLabelWidth = CGFloat(42.0)
// #pragma mark - Object Lifecycle
init(frame: CGRect, person: Person, options: MDCSwipeToChooseViewOptions) {
super.init(frame: frame)
self.person = person
self.imageView.image = self.person.image // self.imageView is nil.
self.autoresizingMask = .FlexibleHeight | .FlexibleWidth | .FlexibleBottomMargin
self.imageView.autoresizingMask = self.autoresizingMask
self.constructInformationView()
}
....
}