代码之家  ›  专栏  ›  技术社区  ›  Daddy

iPhone导航控制器堆栈的nscoding序列化

  •  0
  • Daddy  · 技术社区  · 15 年前

    编辑:我似乎找到了一些有用的东西。我保留了IVAR的“堆栈”,现在它似乎在工作

    我已经连续化了几个自定义nsObject类,没有问题。现在我想序列化我的导航控制器堆栈。为了重建导航树,每个视图控制器只需要保存几个属性。我已经在视图控制器中实现了nscoding协议,并成功地将它们编码为nsdata并保存到磁盘。

    当我尝试加载堆栈时,得到的数组具有正确的对象数,但是当我尝试设置viewcontroller数组时,我总是得到exc ou bad ou访问错误。我是不是走错了路?

    //AppDelegate.m
    -(void) loadDataFromDisk {
       NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
       NSString *programDataPath = [libraryPath stringByAppendingPathComponent:@"programData.dat"];
       NSData *programData = [[NSData alloc] initWithContentsOfFile:programDataPath];
       NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:programData];
       //stack is a mutable array declared in header
       //stack = [decoder decodeObjectForKey:@"stack"];
           stack = [[decoder decodeObjectForKey:@"stack"]retain]; //retain fixes? Seems to work
       [decoder release];
    }
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
       // Override point for customization after app launch    
       [window addSubview:[navigationController view]];
       [window makeKeyAndVisible];
       NSLog(@"%@",self.navigationController.viewControllers);
       if ([stack count] > 1) {
               self.navigationController.viewControllers = stack;
               [stack release];  //retained earlier
       }
       return YES;
    

    }

    1 回复  |  直到 14 年前
        1
  •  0
  •   Daddy    14 年前

    我必须在从磁盘加载后保留viewcontroller堆栈。显然,如果您不立即将数据分配给保留的属性,它就会消失。