【发布时间】:2014-07-09 04:24:27
【问题描述】:
我有一个 Objective-C UIView 类:
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
通常我会继承并像下面这样使用它:
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
我面临的问题是,当我尝试在 Swift 中执行此操作时,self.imageView 属性始终为 nil。
我不确定如何重新实现 initWithFrame,因为它是在 Swift 中的 Objective-C 中实现的。我认为使用init(frame: CGRect, person: Person, options: MDCSwipeToChooseViewOptions) 是正确的方法,但我想我可能错了。
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()
}
....
}
【问题讨论】:
标签: uiview swift subclassing