在我的某个应用中,我实施了state restoration,目前正在开发iPad版本。在iPad版本中,我使用UIPopoverController
来显示视图控制器,但我无法了解应如何保存和恢复这些弹出控制器。
UIPopoverController
未从UIViewController
继承,因此没有我可以使用的restorationIdentifier
和restorationClass
属性。
我是否负责保存popover控制器的targetRect
并在我的主视图控制器中手动编码contentViewController
,以便我可以在-decodeRestorableStateWithCoder:
方法中恢复它,或者是否有更简单的方法只是在文档中找不到?
Fabain
答案 0 :(得分: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];
}
}
这个解决方案最好不是最理想的,所以如果有人想出更好的东西,请告诉我!