xcode 将 UITableView DataSource/Delegate 连接到基 UIViewController 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11068498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:37:25  来源:igfitidea点击:

Connect UITableView DataSource/Delegate to base UIViewController class

objective-ciosxcode

提问by Dakine83

I'm attempting to connect my UITableView's DataSource and Delegate to my ViewController's base class via the storyboard. I Was able to connect my IBActions and IBOutlets easily, but this is proving trickier.

我试图通过故事板将我的 UITableView 的数据源和委托连接到我的 ViewController 的基类。我能够轻松地连接我的 IBActions 和 IBOutlets,但事实证明这更棘手。

Is the correct method to instantiate the delegate/datasource methods then call the same methods on super?

实例化委托/数据源方法然后在超级上调用相同方法的正确方法是什么?

TIA

TIA

回答by Chris Trahey

Xcode will type-check the objects you attempt to connect. So there are two pre-requisites:

Xcode 将对您尝试连接的对象进行类型检查。所以有两个先决条件:

  1. An object of your class must "be present" in the storyboard, which usually involves setting a custom class on a UIViewController in the inspector
  2. The class of your object must declare that it conforms to the correct protocols before you make the connections.
  1. 您的类的对象必须“存在”在故事板中,这通常涉及在检查器中的 UIViewController 上设置自定义类
  2. 在建立连接之前,对象的类必须声明它符合正确的协议。

If those two criteria are met, you should be able to ctrl-click on the UITableViewController, and drag from these connections to an object of your class, either in the storyboard or in the list of objects on the left.

如果满足这两个条件,您应该能够按住 ctrl 单击 UITableViewController,然后从这些连接拖动到您的类的对象,在故事板或左侧的对象列表中。

EDIT: I have added a picture of setting the delegates via the two-step method of ctrl-clicking the table view, then dragging from the popover to the protocol-conforming view controller. This technique works with both the list view on the left as well as the graphical representations in the workspace.

编辑:我添加了一张通过 ctrl 单击表视图,然后从弹出窗口拖动到符合协议的视图控制器的两步方法设置代表的图片。此技术适用于左侧的列表视图以及工作区中的图形表示。

Setting a table view's delegates

设置表视图的委托