ios - 自定义 UIView 并在访问属性时崩溃

标签 ios objective-c uiview

所以我有一个子类UIView:

.h
@interface HistogramGraphPanel : UIView

@property (strong, nonatomic) UIView *graphView;

-(id)initWithDataset:(Dataset *)dataset;

@end

.m
-(id)initWithDataset:(Dataset *)dataset {
    self = [super init];
    if (self) {
        self.dataset = dataset;

        self.translatesAutoresizingMaskIntoConstraints = FALSE;

        UIView *contentView = [UIView new];
        contentView.translatesAutoresizingMaskIntoConstraints = FALSE;
        contentView.backgroundColor = [UIColor orangeColor];
        contentView.tag = 6;
        self.contentView = contentView;

        UIView *headerView = [UIView new];
        headerView.translatesAutoresizingMaskIntoConstraints = FALSE;
        headerView.backgroundColor = [UIColor blueColor];
        self.headerView = headerView;

        [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView(60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]];

        [contentView addSubview:headerView];

        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]];
        [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]];

        UILabel *headerLabel = [UILabel new];
        headerLabel.translatesAutoresizingMaskIntoConstraints = FALSE;
        headerLabel.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:24];
        headerLabel.text = @"Analysis Histogram";
        headerLabel.backgroundColor = [UIColor clearColor];

        [headerView addSubview:headerLabel];

        [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[headerLabel]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerLabel)]];
        [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[headerLabel]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerLabel)]];

        [self createGraphView];

        self = (HistogramGraphPanel *)contentView;
    }
    return self;
}

创建和使用 UIView 子类:

HistogramGraphPanel *graphPanel = [[HistogramGraphPanel alloc] initWithDataset:dataset];

[self.view addSubview:graphPanel];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[panel]-[graphPanel]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(graphPanel, panel)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(49)-[graphPanel]-(228)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(panel, graphPanel)]];

DLog(@"graphPanel.graphView: %@", graphPanel.graphView);

在我尝试访问 graphPanel.graphView 之前效果很好:

2013-04-25 16:44:58.579 [15666:907] -[UIView graphView]: unrecognized selector sent to instance 0x1e0c8320
2013-04-25 16:44:58.580 [15666:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView graphView]: unrecognized selector sent to instance 0x1e0c8320'

实例 0x1e0c8320 是我创建的 graphView 的 contentView。

如果我不尝试访问该属性,它将运行而不会崩溃。

有什么理想吗?

最佳答案

问题出在您的初始化方法中。

为什么你要用 self = (HistogramGraphPanel *)contentView; 做所有事情? contentView 不是一个 graphView,但是你用你的 cast 给它设置了 self。我认为如果您只是省略该行,您的代码应该可以工作。

一般来说,将 self 赋值给 [super init] 返回的内容以外的任何东西都不是一个好主意,正如你可能看到的,当编译器没有那个 Actor 会给你一个警告/错误。

关于ios - 自定义 UIView 并在访问属性时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16225285/

相关文章:

php - 移动在线iOS应用程序的良好教程或指南

ios - 如何在 Swift 类中导入现有的 Objective C PCH 文件

iphone - MonoTouch——我们应该使用企业许可证还是专业许可证?

ios - 尝试范围信标时出现“CoreBluetooth [API MISUSE] CBCentralManager仅在开机状态下接受命令”错误

swift - 如何在 UIScrollView 移动时正确转换 UIView 的比例

ios - UIView 线性动画移动 View

ios - 动态添加自定义 UITableViewCell

objective-c - objective c linux 编译脚本

iOS Firebase Pod - 找不到框架 FirebaseCore 错误

iphone - 在touchesMoved方法中检测[UITouch view]