2013-08-23 75 views
2

在我的一個應用程序中,我實現了state restoration,目前正在研究iPad版本。在iPad版本中,我使用UIPopoverController來顯示視圖控制器,但我無法找出如何保存和恢復這些彈出控制器。如何使用iOS狀態還原恢復UIPopoverController?

UIPopoverController不從UIViewController繼承,因此沒有restorationIdentifierrestorationClass屬性我可以利用。

我是負責保存酥料餅控制器的targetRect並將其編碼手動的contentViewController在我的主視圖控制器,所以我可以其-decodeRestorableStateWithCoder:方法時將其恢復或有更簡單的方法,我只是不能在文檔中找到?

Fabain

回答

2

既然你有用於存儲有關當前正呈現的酥料餅控制器的其他信息的屬性popoverControllerRestorationDictionary,這裏是你如何能恢復酥料餅控制器的想法:

- (void)presentPopoverController:(UIPopoverController *)popoverController fromRect:(CGRect)rect animated:(BOOL)animated 
{ 
    [popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:animated]; 

    self.popoverControllerRestorationDictionary = @{ @"popoverController" : popoverController, @"rect" : [NSValue valueWithCGRect:rect] }; 
} 

- (void)dismissedPresentedPopoverController 
{ 
    self.popoverControllerRestorationDictionary = nil; 
} 

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder 
{ 
    ... 

    if (self.popoverControllerRestorationDictionary != nil) { 
     [coder encodeBool:YES forKey:@"restorePopoverController"]; 

     UIPopoverController *popoverController = [self.popoverControllerRestorationDictionary objectForKey:@"popoverController"]; 
     [coder encodeObject:popoverController.childViewController forKey:@"popoverController.childViewController"]; 

     NSValue *rectValue = [self.popoverControllerRestorationDictionary objectForKey:@"rect"]; 
     [coder encodeObject:rectValue forKey:@"popoverController.targetRect"]; 
    } 
} 

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder 
{ 
    ... 

    if ([coder decodeObjectForKey:@"restorePopoverController"]) { 
     UIViewController *childViewController = [coder decodeObjectForKey:@"popoverController.childViewController"]; 
     CGRect targetRect = [[coder decodeObjectForKey:@"popoverController.targetRect"] CGRectValue]; 

     UIPopoverController *popoverController = [[UIPopoverController alloc] initWithChildViewController:childViewController]; 

     [self presentPopoverController:popoverController fromRect:targetRect animated:NO]; 
    } 
} 

該解決方案是不太理想,所以如果有人想出更好的東西,請告訴我!