2013-04-25 2 views
0
은 그래서 나는 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입니다.

속성에 액세스하려고 시도하지 않으면 충돌로 실행됩니다.

이상형?

+2

바보 같은 질문, '@ graphView를 합성 했습니까?' –

+2

최신 컴파일러에서는 @synthesize가 더 이상 필요하지 않습니다. – mrueg

+0

mrueg가 말한 것. – Padin215

답변

3

문제는 init 메소드에 있습니다.

self = (HistogramGraphPanel *)contentView;으로 모든 작업을 수행하는 이유는 무엇입니까? contentView은 그래프보기가 아니지만 캐스트로 self을 설정했습니다. 나는 당신이 단순히 그 라인을 빠뜨리면 코드가 작동한다고 생각한다.

[super init]이 반환하는 것 이외의 값으로 self을 할당하는 것은 일반적으로 좋지 않습니다. 컴파일러에서 해당 캐스트가없는 경고/오류가 발생했을 때 알 수 있습니다.

+0

contentView를 자기에게 할당하지 않으면 아무 것도 나타나지 않습니다. 코드가 실행되면서 충돌이 발생합니다. 나는 대신 [자기 addsubView :]'합니까? – Padin215

+0

좋아, 나는'[self addSubview]'를 시도하고 제약 조건을 설정하고 매력처럼 작동한다. 'self = contentView'가 틀렸다고 지적 해 주셔서 감사합니다. – Padin215

관련 문제