代表不工作

时间:2011-03-04 18:05:04

标签: iphone objective-c ios

我在viewController中有这个代码:

- (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只为self.graphview.delegate说(null) 即使graphModel中的NSLog说我成功创建了一个对象。怎么会这样?

这是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

中有@synthesize delegate

1 个答案:

答案 0 :(得分:3)

最有可能的猜测:graphView是零。在nil对象上调用任何方法都没有效果并返回nil,而.delegate实际上是对getter或setter的调用。我建议你添加:

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

快速验证。