我一直在尝试使用情节提要创建此代码的版本:
我希望能够在两个不同的详细视图之间切换,具体取决于导航表中选择的单元格。我试图通过使用自定义设置器方法创建一个 SplitViewManager 来实现这一点,该方法在每次选择不同的单元格时换出详细视图。这与 Apple 的示例代码使用的方法相同。SplitViewManager 跟随委托。
我认为我的问题是我没有将 splitViewController.delegate 连接到任何东西,所以我也不能将 splitViewManager 分配给任何东西。但我无法弄清楚我什至会将代表与情节提要中的什么联系起来。如果我在这里是个白痴(几乎肯定),请告诉我。谢谢!
我的代码如下:
DFMAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.splitViewController = (UISplitViewController *)self.window.rootViewController;
self.splitViewManager = (DFMSplitViewManager *)self.splitViewController.delegate;
NSLog(@"split view controller: %@", self.splitViewController); // not null
NSLog(@"split view controller delegate: %@", self.splitViewController.delegate); // is null
NSLog(@"split view manager: %@", self.splitViewManager); // is null.
// But i'm not sure how to assign splitViewController.delegate or splitViewManager in the storyboard.
return YES;
}
DFMSplitViewManager.m:
- (void)setDetailViewController:(UIViewController<SubstitutableDetailViewController> *)detailViewController
{
self.detailViewController = detailViewController;
// Update the split view controller's view controllers array.
// This causes the new detail view controller to be displayed.
UIViewController *navigationViewController = [self.splitViewController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:navigationViewController, self.detailViewController, nil];
self.splitViewController.viewControllers = viewControllers;
}
DFMMasterViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DFMAppDelegate *appDelegate = (DFMAppDelegate *)[[UIApplication sharedApplication] delegate];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if (indexPath.row == 0) {
NSLog(@"clicked cell 1");
DFMDetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[appDelegate.splitViewManager setDetailViewController:detailViewController];
}
else {
NSLog(@"clicked cell 2");
DFMDetailCollectionViewController *detailCollectionViewController = [storyboard instantiateViewControllerWithIdentifier:@"CollectionViewController"];
[appDelegate.splitViewManager setDetailViewController:detailCollectionViewController];
}
}