I answer this without reading all the comments.
There is no problem. Both AViewController ( AVC ) and BViewController ( BVC ) have their own private properties named volly .
You have created an instance of BVC . He cannot see the volly property from his parent class (because it is private), only his own.
Now the fun begins.
The viewDidLoad method from the BVC called. It, in turn, calls [super viewDidLoad]; Which, of course, calls viewDidLoad from the AVC class. This method, in turn, calls self.volly = 5; .
The confusion seems to be with this line. Remember that self.volly = 5; is really a challenge:
[self setVolly:5]
Both AVC and BVC have a (synthesized) setVolly: method. Since self is a pointer to an instance of a BVC object, the call to [self setVolly:5]; leads to a call to the setVolly: method in the BVC class, although it is called from a method in the AVC class.
Here is the code with some annotations:
Class "BVC":
- (void)viewDidLoad { [super viewDidLoad]; // calls the method in `AVC` NSLog(@"%d", self.volly); // returns 5 }
Class "AVC":
- (void)viewDidLoad { [super viewDidLoad];
In the end, the subclass does not use the private property of the parent class. The original premise in the question header is incorrect.
rmaddy
source share