Saturday, 28 September 2013

Xcode IOS: a property of viewcontroller disappears after viewDidLoad has been called

Xcode IOS: a property of viewcontroller disappears after viewDidLoad has
been called

I am writing an app for iphone (iOS 7, Xcode5), and found some strange
behaviour from one of the viewController classes
(TrackDetailsViewController):
When this view is getting called i pass a Track item with some information
i deed through the prepareForSegue method. Now When i check for the Track
Item in the viewDidLoad method it exists and has all the data i expect it
to have. However, when I check it again in the viewDidAppear the Track
Item has become zero (see code below):
TrackDetailsViewController.h:
@interface TrackDetailsViewController : UIViewController
<UIScrollViewDelegate>
@property (weak, nonatomic) Track *track;
@end
TrackDetailsViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
//check for track
if (_track !=nil) {
NSLog(@"track not nil");
}
else{
NSLog(@"no track");
}
// Do any additional setup after loading the view.
}
-(void) viewDidAppear: (BOOL) animated{
//check for track
if (track != nil) {
NSLog(@"track not nil");
}
else{
NSLog(@"no track");
}
}
Now the log after running this code (after showing the view in question)
looks like:
2013-09-28 16:25:31.877 I-Sail[1388:c07] track not nil
2013-09-28 16:25:32.235 I-Sail[1388:c07] no track
So, somewhere in-between viewDidLoad and viewDidAppear, the value of the
track property was changed to null.
After that the whole thing becomes even weirder to me, since the track
property keeps its value when i add a variable track2 to the .m file, and
assign the value of the track property to the track2 variable like shown
in the code below:
Track *track2;
- (void)viewDidLoad
{
[super viewDidLoad];
//check for track
if (_track !=nil) {
NSLog(@"track not nil");
}
else{
NSLog(@"no track");
}
//assign the value of _track to the track2 variable
track2 = _track;
}
-(void) viewDidAppear: (BOOL) animated{
//check for track
if (_track !=nil) {
NSLog(@"track not nil");
}
else{
NSLog(@"no track");
}
}
This time the output becomes:
2013-09-28 16:35:57.929 I-Sail[1452:c07] track not nil
2013-09-28 16:35:58.286 I-Sail[1452:c07] track not nil
So the property track keeps its value and i can use it in other methods of
the viewController class as well.
Has anybody experienced issues like this before, some explanation for this
behaviour? or found a more elegant workaround than creating a dummy
variable?
Cheers!

No comments:

Post a Comment