In the setProgram
and setGraphic
methods we have the same code:
NSLog(@"%@", self.display);
But the output shows that self.display is null in setProgram
, but is a UILable object in setGraphic
.
What causes the different result?display
is IBOutlet and program
is NSArray object. While program
is the model of the SOGraphicViewController, I will pass the value of program from another controller by [segue.destinationViewController setProgram: self.brain.program]
.
@interface SOGraphicViewController ()
@property (nonatomic, weak) IBOutlet graphicView *graphic;
@end
@implementation SOGraphicViewController
@synthesize program = _program;
@synthesize graphic = _graphic;
@synthesize display = _display;
- (void)setProgram:(id)program {
_program = program;
[self.graphic setNeedsDisplay];
NSLog(@"%@", self.display);
}
- (void)setGraphic:(graphicView *)graphic {
_graphic = graphic;
self.graphic.dataSource = self;
NSLog(@"%@", self.display);
}
Assuming that your properties are all
IBOutlets
, then the best guess is that the setters are being called during XIB loading anddisplay
is set in betweenprogram
andgraphic
being set.That is, as the XIB is unarchived, the setters are being called as the objects are reconstituted. The order is undefined.
(This amused me; I ran into the same confusion when I was first learning about NIB loading ~23 years ago. BTW: Ya'll can get off my lawn.)
Given that those are outlets, there is no need to call
setNeedsDisplay
. In fact, you shouldn't override the setters at all.Also, class names always start with a capital letter.