我一直在尝试使用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];
}
}
答案 0 :(得分:2)
事实证明,您可以使用界面构建器将NSObject添加到View Controllers。一旦我这样做,我将NSObject的类更改为DFMSplitViewManager,将其设置为SplitViewController的委托,并且从那里开始非常简单。
答案 1 :(得分:1)
我面临着与你完全相同的问题。不知道你是否找到了解决方案,这是我找到的解决方案。
在AppDelegate.m中使用以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
self.detailViewManager = [[DetailViewManager alloc] init];
self.detailViewManager.splitViewController = splitViewController;
self.detailViewManager.detailViewController = splitViewController.viewControllers.lastObject;
splitViewController.delegate = self.detailViewManager;
if ([splitViewController respondsToSelector:@selector(setPresentsWithGesture:)])
[splitViewController setPresentsWithGesture:YES];
return YES;
}
其余代码与Apple提供的代码相同。
基本上,self.detailViewManager
是我们的分割视图控制器,当您在表格中选择单元格时,self.detailViewManager
将重置详细视图(如果我没有错)。我是Xcode的新手,所以如果我错了,请任何人纠正我。
这是solution link,由标志回答。