在Swift中将UIView子类化时,initWithFrame出现问题
我有一个Objective-C在Swift中将UIView子类化时,initWithFrame出现问题,uiview,swift,subclassing,Uiview,Swift,Subclassing,我有一个Objective-CUIView课程: 选择sepersonview.h @class Person; @interface ChoosePersonView : MDCSwipeToChooseView @property (nonatomic, strong, readonly) Person *person; - (instancetype)initWithFrame:(CGRect)frame person:(Person *
UIView
课程:
选择sepersonview.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
@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
通常我会子类化并使用它,如下所示:
选择sepersonview.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
@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:mdcswipetochooseviewooptions)
是正确的方法,但我认为我可能错了
选择sepersonview.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()
}
....
}
self.imageView
为零,因为您没有采取任何措施使其不为零。您没有显示任何代码使其不为零,因此很难猜测您认为应该如何实现
我猜是MDCSwipeToChooseView
的初始值设定项的工作,您可以将其称为super.init(frame:options:)
,以创建imageView
。很难说,因为您没有显示代码。当然,当ChoosePersonView是用Objective-C编写的时候,您调用的是初始值设定项。既然它是用Swift编写的,为什么现在不调用它呢?出于某种原因,您选择不调用该初始值设定项—而是调用super.init(frame:)
。这样做似乎很奇怪。也许这就是你错的地方
但在任何情况下,我可以向您保证,如果您不调用导致
imageView
不为零的代码,它将为零,因为这是Objective-C中所有对象属性的默认值。否,在Objective-C中它不是“已实例化”。正如我所说,您没有显示的某些代码一定是在实例化它。无论您使用哪种语言,事物本身都不会实例化。所以问题是我调用的是super.init(frame:frame)
,而不是super.init(frame:frame,options:options)
。这就是初始化必要的ImageView等,但这正是我所说的。你没看过我的答案吗?你接受了,但你甚至没有读,也没有按照我的建议去做?那有什么意义?别紧张,伙计。什么给你的印象是我没有读你的答案?我添加了一条评论,只是为了确认你的答案的正确性。你今天过得不好吗?