状态恢复和保存,编码UILabel

时间:2014-05-27 10:23:12

标签: ios decode encode state-restoration uikit-state-preservation

-(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)来自控制台。 请帮忙。

0 个答案:

没有答案