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

在Swift中继承UIView时使用initWithFrame的问题,第1张

概述我有一个Objective-C UIView类: ChoosePersonView.h @class Person;@interface ChoosePersonView : MDCSwipeToChooseView@property (nonatomic, strong, readonly) Person *person;- (instancetype)initWithFrame:( 我有一个Objective-C UIVIEw类:

ChoosePersonVIEw.h

@class Person;@interface ChoosePersonVIEw : MDCSwipe@R_502_6091@ooseVIEw@property (nonatomic,strong,Readonly) Person *person;- (instancetype)initWithFrame:(CGRect)frame                       person:(Person *)person                      options:(MDCSwipe@R_502_6091@ooseVIEwOptions *)options;@end

MDCSwipe@[email protected]

@class MDCSwipe@R_502_6091@ooseVIEwOptions;@interface MDCSwipe@R_502_6091@ooseVIEw : UIVIEw@property (nonatomic,strong) UIImageVIEw *imageVIEw;@property (nonatomic,strong) UIVIEw *likedVIEw;@property (nonatomic,strong) UIVIEw *nopeVIEw;- (instancetype)initWithFrame:(CGRect)frame                      options:(MDCSwipe@R_502_6091@ooseVIEwOptions *)options;@end

通常我会子类化并使用它如下:

ChoosePersonVIEw.m

@implementation ChoosePersonVIEw#pragma mark - Object lifecycle- (instancetype)initWithFrame:(CGRect)frame                       person:(Person *)person                      options:(MDCSwipe@R_502_6091@ooseVIEwOptions *)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:MDCSwipe@R_502_6091@ooseVIEwOptions)是正确的方法,但我想我可能是错的.

ChoosePersonVIEw.swift

class ChoosePersonVIEw: MDCSwipe@R_502_6091@ooseVIEw {    var informationVIEw: UIVIEw!    var @R_502_6889@Label: 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: MDCSwipe@R_502_6091@ooseVIEwOptions) {        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是零,因为你没有做任何事情使它不是零.你没有显示任何会使它不是零的代码,所以你很难想象你应该怎么想.

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

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

总结

以上是内存溢出为你收集整理的在Swift中继承UIView时使用initWithFrame的问题全部内容,希望文章能够帮你解决在Swift中继承UIView时使用initWithFrame的问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1002437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存