3

我一直在尝试使用情节提要创建此代码的版本:

我希望能够在两个不同的详细视图之间切换,具体取决于导航表中选择的单元格。我试图通过使用自定义设置器方法创建一个 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];
    }
}
4

2 回答 2

2

事实证明,您可以使用界面构建器将 NSObjects 添加到视图控制器。完成此操作后,我将 NSObject 的类更改为 DFMSplitViewManager,将其设置为 SplitViewController 的委托,从那里开始非常简单。

于 2013-10-24T17:36:46.693 回答
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 的新手,所以如果我错了,请任何人纠正我。

这是解决方案链接,由标志性回答。

于 2014-03-19T21:19:54.510 回答