State restoration and preservation , encoding UILabel

210 Views Asked by At
-(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];


}

The code above is used to encode all the UILabel that I added as subview onto the screen through a for loop. However when decoding the only thing i get is (null) from the console. Please help.

0

There are 0 best solutions below