我一直在尝试使用storyboard创建此代码的版本:
我希望能够在两个不同的详细信息视图之间切换,具体取决于在导航表中选择的单元格。我尝试通过使用自定义setter方法创建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];
}
}
最佳答案
原来,您可以使用界面生成器将NSObject添加到View Controllers。完成此操作后,我将NSObject的类更改为DFMSplitViewManager,将其设置为SplitViewController的委托,并且从那里开始很简单。