当前位置 : 主页 > 手机开发 > 其它 >

在Swift中继承UIView时使用initWithFrame的问题

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个Objective-C UIView类: ChoosePersonView.h @class Person;@interface ChoosePersonView : MDCSwipeToChooseView@property (nonatomic, strong, readonly) Person *person;- (instancetype)initWithFrame:(CGRect)frame person:(Person *)pers
我有一个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()
    }
    ....
}
self.imageView是零,因为你没有做任何事情使它不是零.你没有显示任何会使它不是零的代码,所以你很难想象你应该怎么想.

我的猜测是,它是MDCSwipeToChooseView的初始化程序的工作,您可以将其称为super.init(frame:options :),以创建imageView.很难说,因为你没有显示代码.当然,这是在Objective-C中编写ChoosePersonView时调用的初始化程序.那么为什么你现在也没有调用它,现在它是用Swift编写的?出于某种原因,您选择不调用该初始化程序 – 您正在调用super.init(frame :).这似乎是一件非常奇怪的事情.也许这就是你出错的地方.

但无论如何,我可以向你保证,如果你不调用一些导致imageView不为nil的代码,它将是nil,因为这是Objective-C中所有对象属性的默认值.

网友评论