iOS - 带有故事板的 UISplitViewController - 多个主视图和多个细节视图 [英] iOS - UISplitViewController with storyboard - multiple master views and multiple detail views

查看:19
本文介绍了iOS - 带有故事板的 UISplitViewController - 多个主视图和多个细节视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UISplitViewController 和故事板组合一个 iPad 应用程序.主视图以导航控制器链接到 6 个菜单选项的表格视图开始.表格中的每个单元格将不同的表格视图控制器推送到导航堆栈上.这适用于主视图.每个主视图都有一个表列表,单击该列表时需要在详细信息窗格中显示不同的视图控制器.我目前已将 segue 设置为替换"和详细信息拆分",这在第一次单击行时起作用,但是一旦您单击主视图中的另一行或旋转设备,应用程序就会崩溃与 EXC_BAD_ACCESS.

I'm trying to put together an iPad app using UISplitViewController and storyboards. The master view starts with a navigation controller linked to a table view of 6 menu options. Each cell in the table pushes a different table view controller onto the navigation stack. This is working fine for the master view. Each master view has a table list which when clicked needs to display a different view controller in the detail pane. I've currently done this with a segue set to 'Replace' and 'Detail Split' which works the first time a row is clicked, but as soon as you click another row in the master view, or rotate the device then the app crashes with EXC_BAD_ACCESS.

我很确定我的问题与如何为 UISplitViewController 设置委托有关.当我有多个主 VC 和多个细节 VC 时,我对如何使用它感到困惑.委托代码应该放在哪里——主代码还是细节代码?我是否必须在每个视图控制器中实现 UISplitViewControllerDelegate 协议事件?

I'm fairly sure my problems are to do with how the delegate is setup for the UISplitViewController. I'm confused as to how this should be used when I have multiple master VCs and multiple detail VCs. Where should the delegate code be placed - master or detail? Do I have to implement the UISplitViewControllerDelegate protocol events in every view controller?

感谢任何帮助.

推荐答案

如果拆分视图控制器委托是被替换的细节视图控制器,这就是崩溃的原因.被替换的细节视图控制器正在被释放,因此拆分视图控制器委托不再是对有效对象的引用.

If the split view controller delegate was the detail view controller that had been replaced, this is the cause of the crash. The replaced detail view controller is being dealloc'd and so the split view controller delegate is no longer a reference to a valid object.

您可以在 prepareForSegue:sender: 中更新委托.例如:

You can update the delegate in prepareForSegue:sender:. For example:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {
        UIViewController *destinationViewController = [segue destinationViewController];
        if ([destinationViewController conformsToProtocol:@protocol(UISplitViewControllerDelegate)]) {
            self.splitViewController.delegate = destinationViewController;
        }
        else {
            self.splitViewController.delegate = nil;
        }
    }
}

您用于委托的视图控制器取决于您的视图控制器层次结构.在最简单的情况下,分配给 splitVC detail 的任何视图控制器可能需要是委托.您可能希望将它们全部基于处理共享拆分视图控制器委托逻辑的公共超类.

Which view controllers you use for delegates is dependent on your view controller hierarchy. In the simplest case, any view controllers that are assigned to splitVC detail will probably need to be delegates. You may want to base them all on a common super class that handles the shared split view controller delegate logic.

这篇关于iOS - 带有故事板的 UISplitViewController - 多个主视图和多个细节视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆