-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(@"Start encode");
for (int index = 1; index <= [self.view.subviews count]-1; index++) {
NSString *encodeKey = [NSString stringWithFormat:@"%dnumber",index];
UILabel *label = self.view.subviews[index];
[coder encodeObject:label forKey:encodeKey];
NSLog(@"%@",label);
}
[coder encodeInteger:[self.view.subviews count] forKey:@"count"];
[super encodeRestorableStateWithCoder:coder];
NSLog(@"End encode");
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
NSUInteger number = [coder decodeIntegerForKey:@"count"];
NSLog(@"Start decode");
for (int index = 1; index <= number-1; index++) {
NSString *encodeKey = [NSString stringWithFormat:@"%dnumber",index];
UILabel *label = [coder decodeObjectForKey:encodeKey];
NSLog(@"%@",label);
[self.view addSubview:label];
}
NSLog(@"End decode");
[super decodeRestorableStateWithCoder:coder];
}
上面的代码用于通过for循环将我作为子视图添加的所有UILabel编码到屏幕上。但是当解码时我得到的唯一东西是(null)来自控制台。 请帮忙。