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。

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

有理想吗?

最佳答案

问题出在您的init方法中。

为什么用self = (HistogramGraphPanel *)contentView;进行整个操作? contentView不是graphView,但是您可以通过强制转换将self设置为它。我认为,如果您仅省去该行,那么您的代码应该可以工作。

通常,将self分配给除[super init]返回的内容之外的任何内容都不是一个好主意,因为您可能会看到编译器在没有该强制类型的情况下向您发出警告/错误。

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

相关文章:

ios - 按位置和缩放级别向服务器请求 mapkit 注释

iphone - 添加自定义 UITableViewCell 会使模拟器崩溃

ios - dispatch_group_async性能

iphone - 如何将另一个 Class 文件中的 IBOutlet 使用到不同的 .m 文件中?

ios - 如何在iOS中获取动画UIView的当前位置

iphone - Interface Builder 中的组 View

ios - 在 View Controller 上启用滑动,但在该 VC 的 View 中禁用

ios - 从视频文件中删除音频

ios - 如果忘记密码,如何恢复 .pem 文件

ios - 在 Swift 中更改 UITextView 的文本颜色