Новое для iOS / Objective-C здесь. Я потратил много времени, пытаясь понять это, но просто не могу с этим справиться. Вот что происходит:
- Нажмите кнопку «График» в представлении моего RootViewController
- GraphViewController вступает во владение, имеет свойство graphView
- Я установил в свойстве dataSource для GraphView значение self
- Проверка self.graphView.dataSource с NSLog подтверждает, что он действительно указывает на себя
- Когда вызывается drawRect: в GraphView, self.dataSource устанавливается в (null), где я ожидал, что он будет указывать на объект GraphViewController
Подводя итог: я создаю экземпляр свойства graphView в контроллере представления, затем устанавливаю его dataSource, но к тому времени, когда viewRect: вызывается, dataSource больше не устанавливается.
GraphViewController.m:
#import "GraphViewController.h"
#import "GraphView.h"
@implementation GraphViewController
@synthesize graphView = _graphView;
@synthesize program = _program;
- (GraphView *)graphView {
if(!_graphView) {
_graphView = [[GraphView alloc] init];
[_graphView setDataSource:(id)self];
}
return _graphView;
}
@end
GraphViewController.h:
#import <UIKit/UIKit.h>
#import "GraphView.h"
@interface GraphViewController : UIViewController
@property (strong, nonatomic) IBOutlet GraphView *graphView;
@end
GraphView.m:
#import "GraphView.h"
@implementation GraphView
@synthesize dataSource = _dataSource;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(@"initWithFrame called, self.dataSource=%@", self.dataSource);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawRect:");
NSLog(@"\tself=%@", self);
NSLog(@"\tself.dataSource=%@", self.dataSource); // is (null), shouldn't be
[self.dataSource programToGraph];
}
@end
GraphView.h:
#import <UIKit/UIKit.h>
@class GraphView;
@protocol GraphViewDataSource
- (id)programToGraph;
@end
@interface GraphView : UIView
@property (nonatomic, weak) IBOutlet id <GraphViewDataSource> dataSource;
@end