到目前爲止,我已經解決了這樣的問題:在viewWillAppear所有的細節控制器都從標籤欄的導航控制器中刪除。 當存儲狀態時,設備方向被保存並且SplitViewController.viewControllers(對我來說它不會像iOS7那樣自動存儲它們)。
- (void) encodeRestorableStateWithCoder:(NSCoder*)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.viewControllers forKey:@"viewControllers"];
[coder encodeInteger:[UIApplication sharedApplication].statusBarOrientation forKey:@"orientation"];
}
- (void) decodeRestorableStateWithCoder:(NSCoder*)coder
{
[super decodeRestorableStateWithCoder:coder];
NSArray* viewControllers = [coder decodeObjectForKey:@"viewControllers"];
if (viewControllers.count > 0)
{
self.viewControllers = viewControllers;
}
restoredOrientation = (UIInterfaceOrientation) [coder decodeIntegerForKey:@"orientation"];
}
這裏是viewWillAppear中執行:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// iPhone6+: if state restoration happened while in portrait orientation and app is launched while in landscape,
// then all detail views should be cut from master view and split details view is set appropriately
if (firstLoad &&
[UIScreen mainScreen].scale > 2.9 && // assure it's iPhone 6+
UIInterfaceOrientationIsPortrait(restoredOrientation) &&
UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation))
{
UITabBarController* tbc = self.viewControllers[0];
NSArray* detachedControllers = [tbc cutControllersFrom:DocumentViewController.class];
if (detachedControllers.count > 0)
{
UINavigationController* documentNavigation = [self.storyboard
instantiateViewControllerWithIdentifier:@"NavigationController"];
documentNavigation.viewControllers = detachedControllers;
self.viewControllers = @[ self.viewControllers[0], documentNavigation ];
}
else // place some default no-selection controller in detail
{
UINavigationController* noSelectionNavigation = [self.storyboard
instantiateViewControllerWithIdentifier:@"NoSelectionSID"];
self.viewControllers = @[ self.viewControllers.firstObject, noSelectionNavigation ];
}
}
firstLoad = NO;
}
其中cutControllersFrom方法是對的UITabBarController類別:
- (NSArray*) cutControllersFrom:(Class)controllerClass
{
NSArray* ret;
for (UIViewController* vc in self.viewControllers)
{
if (![vc isKindOfClass:UINavigationController.class])
{
continue;
}
UINavigationController* nc = (UINavigationController*) vc;
NSArray* removed = [nc cutFrom:controllerClass];
if (vc == self.selectedViewController)
{
ret = removed;
}
}
return ret;
}
它調用cutFrom:方法是在UINavigationController的一個類別:
- (NSArray*) cutFrom:(Class)controllerClass
{
NSMutableArray* toRemove = [NSMutableArray array];
BOOL startRemoving = NO;
UIViewController* endingViewController;
for (NSUInteger i = 0; i < self.viewControllers.count; i++)
{
UIViewController* vc = self.viewControllers[i];
if ([vc isKindOfClass:controllerClass])
{
startRemoving = YES;
endingViewController = self.viewControllers[i - 1];
}
if (startRemoving)
{
[toRemove addObject:vc];
}
}
if (endingViewController)
{
[self popToViewController:endingViewController animated:NO];
}
return toRemove;
}
似乎你沒有什麼選擇,並且在搞清楚所有這些方面做得很好。如果你想改變方法,一個選擇是不使用標籤欄並移動到側欄。我在我當前的應用程序中執行此操作,其中一個側面菜單允許我在多個分割視圖控制器之間切換好東西是每個分割視圖是分開的,並且具有用於主控和細節的導航控制器,因此更簡單。所以你的標籤欄可以成爲側欄菜單,讓你在4個分割視圖控制器之間切換:每個ControllerX一個。只是一個想法。 – 2015-02-26 23:39:59
啊,是的,側面菜單是neet的想法。現在,我必須堅持使用TabBar,但爲了將來的參考,記住它是非常好的。 – mixtly87 2015-02-28 01:08:08