子类 UISplitViewController 并将您的根 splitViewController 设置为该类。然后将此方法添加到您的 UISplitViewController 子类中:
-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called
if (title) {
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:detailController animated:YES];
detailController.title = title;
NSArray *viewControllers=@[self.mainController.viewControllers[0],navController];
self.mainController.viewControllers = viewControllers;
} else {
NSArray *viewControllers=@[self.mainController.viewControllers[0],detailController];
self.mainController.viewControllers = viewControllers;
}
}
要调用此方法,请从主视图控制器执行以下操作:
FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;
CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:@"Current Event"];
我的很多项目都需要 splitviewcontroller 始终显示主视图,所以我使用这个子类来防止主视图在纵向旋转时隐藏。