uiview - 在 Swift 中子类化 UIView 时出现 initWithFrame 问题

标签 uiview swift subclassing

我有一个 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 是 nil,因为你没有做任何事情让它不为 nil。您没有显示任何会使它不为零的代码,因此很难猜测您想象中应该如何发生。

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

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

关于uiview - 在 Swift 中子类化 UIView 时出现 initWithFrame 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645381/

相关文章:

ios - 防止 UIView 重叠

iphone - 如何将 NIB 文件的使用引入我现有的自定义 UIView?

iphone - iOS 根据设备方向切换 View Controller

iphone - UIView.subviews 和 [NSView subviews] 之间的行为差​​异

swift - 如何使用 JSQMessagesViewController 发送图像?

swift - 如何将 UIView 子类化并在其中添加另一个具有相同框架的 UIView

python - 从 dict 派生时,重载的 __iter__ 被绕过

ios - 在恢复之前修改 Alamofire 请求的 header ()d

xcode swift 应用程序,位置在模拟器上有效,但在实际设备上无效

ios - Spritekit 子类节点显示为 nil