ios - Object of array in new ViewController keeps empty -
i've problem array. there's array objects of class car in carviewcontroller:
...car.h
@interface car : nsobject @property (nonatomic, strong) nsstring *name;
..and car.m
- (void)encodewithcoder:(nscoder *)coder; { [coder encodeobject:name forkey:@"name"]; } - (id)initwithcoder:(nscoder *)coder; { self = [[car alloc] init]; if (self != nil) { name = [coder decodeobjectforkey:@"name"]; } return self; }
and carviewcontroller
car *car1 = [car new]; car1.name = @"a1"; ... cars = [nsarray arraywithobjects: car1, car2, ..., nil];
but when try have access array in newviewcontroller there problem:
- (ibaction)btn:(id)sender { uistoryboard *storyboard = [uistoryboard storyboardwithname:@"storyboard" bundle:nil]; carviewcontroller *vc = (carviewcontroller *)[storyboard instantiateviewcontrollerwithidentifier:@"carvc"]; car *car = [vc.cars objectatindex:0]; nslog(@"%@", car.name); }
but in log written car.name = (null). in advance effort.
update:
- (ibaction)btn:(id)sender { uinavigationcontroller *nav = self.tabbarcontroller.viewcontrollers[1]; carviewcontroller *vc = nav.topviewcontroller; car *car = [vc.cars objectatindex:0]; nslog(@"%@", car.name); }
i've tried (thanks rdelmar effort), result still same.
if put code
car *car1 = [car new]; car1.name = @"a1"; ... cars = [nsarray arraywithobjects: car1, car2, ..., nil];
in viewdidload
method of view controller, the result normal because view not loaded yet. (the viewdidload
method has not been called yet)
i'm not familiar storyboards think when call
carviewcontroller *vc = (carviewcontroller *)[storyboard instantiateviewcontrollerwithidentifier:@"carvc"];
it instantiate new viewcontroller. if try access cars
property nil if new viewcontroller not presented on screen
apple documentation
you use method create view controller objects want manipulate , present programmatically in application. before can use method retrieve view controller, must explicitly tag appropriate identifier string in interface builder.
this method creates new instance of specified view controller each time call it.
Comments
Post a Comment