ios6 - 在同一 View Controller 中的两个容器之间传递数据

标签 ios6 uistoryboard uistoryboardsegue

我是 iOS 编程的新手,我阅读了很多教程和论坛,但我仍然无法找到管理项目的最佳方式。

我想要的是 iPad 屏幕并排显示 CollectionView 和 TableView。 CollectionView 中的操作应更改 TableView 内容。 SplitViewController 不会完成这项工作,因为拆分的大小是固定的。

现在我正在使用 Storyboard,我创建了一个 ViewController 并在其中添加了两个 ContainerView。每个容器都由 XCode 生成的 segue 链接到 View Controller (LeftViewController 和 RightViewController)。

我正在尝试找出最聪明的方法来管理 LeftViewController 上的操作并将更改发送到 RightViewController。

我想使用看起来更优雅的 Storyboard,但我不确定如何实现它。

最佳答案

假设您知道建立委托(delegate)方法的方法(@protocol,参见 here for links ),关键要素将是在加载容器时获取嵌入在容器中的两个 viewController,将主 viewController 设置为委托(delegate),然后发送当事情发生变化时的消息。对于初学者,如果通信需要在 Controller 之间双向流动,请为每个 VC 设置一个实例变量。

给定 VCPrime、CollectionVC 和 TableVC:

首先,在 Storyboard中,为每个 segues 命名(从 containerView 到 VC)。在 VCPrime 中,实现 prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"collection"]) {
        self.collectionVC = (CollectionVC *)[segue destinationViewController];
        self.collectionVC.delegate = self;
    }
    if ([segue.identifier isEqualToString:@"table"]) {
        self.tableVC = (TableVC *)[segue destinationViewController];
        self.tableVC.delegate = self;
    }
}

您还必须在 VCPrime 中实现委托(delegate)方法,并声明 CollectionDelegate、TableDelegate,或者您命名它们的任何方式。

在 CollectionVC 中,当有人选择某物(或其他东西)时,检查委托(delegate)是否响应您的委托(delegate)方法,然后发送该消息:

if ([self.delegate respondsToSelector:@selector(doSomething)]) [self.delegate doSomething];

然后在调用的方法中修改 TableVC。

这只是一个简要的概述。互联网上到处都是很棒的代码示例。

关于ios6 - 在同一 View Controller 中的两个容器之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020496/

相关文章:

ios - 清除 Storyboard 中使用的标签文本

ios - 在 UITableView 上选择行时委托(delegate)不起作用

ios - 获取对容器 View View 的引用

iphone - 无法识别的选择器已发送到UIRefreshControl

ios - 将 UIImageView 置于 UIScrollView 中居中

ios - 自定义 UICollectionViewLayout 不同单元格大小

ios - 从 Storyboard 设置 IBAction 时获取 'Touch Drag Inside' 距离

status - UIStatusBarStyleBlackTranslucent 在 iOS 6 中不起作用

ios - 我看不到弹出框继续

ios - 从 StoryBoard 获取对 TableViewController 中左按钮的引用