2013-04-25 77 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。

如果我不嘗試訪問該屬性,它將運行w/out崩潰。

任何理想?

+2

愚蠢的問題,你'@synthesize graphView'? – 2013-04-25 21:48:59

+2

對於較新的編譯器,@synthesize不再是必需的。 – mrueg 2013-04-25 22:05:57

+0

什麼mrueg說。 – Padin215 2013-04-25 22:07:11

回答

3

問題出在你的init方法中。

你爲什麼要用self = (HistogramGraphPanel *)contentView;做整件事? contentView不是一個graphView,但你設置self與你的演員。我認爲你的代碼應該工作,如果你只是忽略了這一行。

通常,將self分配給除[super init]之外的任何內容都不是一個好主意,因爲您可能會看到編譯器在沒有該投射的情況下爲您提供警告/錯誤。

+0

如果我不將contentView指定給self,則不會顯示任何內容。代碼確實運行w/out崩潰。我是不是'[self addsubView:]'而已? – Padin215 2013-04-25 22:14:19

+0

好吧,我試過'[self addSubview]'並設置約束條件,並像魅力一樣工作。感謝您指出'self = contentView'不正確。 – Padin215 2013-04-26 13:46:13