我的数组有问题。在我的 CarViewController 中有一个包含 Car 类的一些对象的数组:
...汽车.h
@interface Car : NSObject
@property (nonatomic, strong) NSString *name;
..和汽车.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;
}
和 CarViewController
Car *car1 = [Car new];
car1.name = @"A1";
...
cars = [NSArray arrayWithObjects: car1, car2, ..., nil];
但是当我现在尝试在 NewViewController 中访问这个数组时,出现了一个问题:
- (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);
}
但是在日志中只是写了car.name = (null)。提前感谢您的努力。
更新:
- (IBAction)btn:(id)sender {
UINavigationController *nav = self.tabBarController.viewControllers[1];
CarViewController *vc = nav.topViewController;
Car *car = [vc.cars objectAtIndex:0];
NSLog(@"%@", car.name);
}
我已经尝试了一些东西(感谢rdelmar的努力),但结果还是一样。