2011-03-04 39 views
0

我有這樣的代碼在我的viewController:爲self.graphview.delegate 代表不工作

- (GraphModel *)graphModel 
{ 
    if (!graphModel) { 
     graphModel = [[GraphModel alloc] init]; 
     NSLog(@"graphModel = %@", graphModel); 
    } 
    return graphModel; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.graphView.delegate = [self graphModel]; 
    NSLog(@"self.graphview.delegate = %@", self.graphView.delegate); 
    [self updateUI]; 
} 

但NSLog的只是說(空),即使在NSLog的說是GraphModel我成功創建的對象。怎麼會這樣?

這對graphViewDelegate

@class GraphView; 

@protocol GraphViewDelegate 
- (double)yValueForGraphView:(GraphView *)requestor atPosition:(int)i withPrecision:(int)precision; 
- (double)scaleForGraphView:(GraphView *)requestor; 
@end 

@interface GraphView : UIView { 
    id <GraphViewDelegate> delegate; 
} 

@property (assign) id <GraphViewDelegate> delegate; 

@end 

代碼,然後我在graphView.m

回答

3

最有可能的猜測有@synthesize delegate:graphView是零。在nil對象上調用任何方法都沒有效果,並返回nil,而.delegate實際上是適當地調用getter或setter。我建議你補充:

NSLog(@"self.graphview = %@", self.graphView); 

作爲一個快速驗證。

+0

graphview的內存何時設置?我認爲它是自動完成的? – ladookie 2011-03-04 18:08:50

+1

實例不會自動提供給成員變量。如果你的graphView成員是一個IBOutlet,並且連接到Interface Builder中的某些東西,那麼假設你得到viewDidLoad後應該加載它。如果你像我一樣,你可能已經在Interface Builder中連接了它,但沒有保存你的XIB,切換回Xcode並使用舊版本構建。 – Tommy 2011-03-04 18:11:08

+0

是的,graphView是一個IBOutlet,我只是意識到我忘了在接口生成器中連接它,哈哈。謝謝。 – ladookie 2011-03-04 18:14:25