xcode 将 UIViewController 更改为故事板内的 UITableViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13957346/
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 02:21:50  来源:igfitidea点击:

Change a UIViewController to a UITableViewController inside a storyboard?

objective-ciosxcodeuiviewcontrolleruitableview

提问by Luke

I've made a view in my storyboard which I've now decided I'd rather display its data via static table cells.

我已经在我的故事板中创建了一个视图,我现在决定我宁愿通过静态表格单元格显示它的数据。

I can't use static table views in a UIViewController(Static table views are only valid when embedded in UITableViewController instances). So, I need to convert my existing UIViewControllerto a UITableViewControllersomehow. I've changed the .h file's parent, but that hasn't done it.

我不能在UIViewController( Static table views are only valid when embedded in UITableViewController instances) 中使用静态表视图。所以,我需要以某种方式将我的现有转换UIViewController为一个UITableViewController。我已经更改了 .h 文件的父级,但这还没有完成。

Is there another way to get this going? I'd really rather not have to make a new VC in the storyboard and move everything over, it's a big hassle.

有没有另一种方法可以实现这一目标?我真的宁愿不必在故事板中创建一个新的 VC 并将所有内容都移过去,这是一个很大的麻烦。

采纳答案by rdelmar

If you want your static cell table view not to take up the entire screen, then using a container view is the easiest way to go. Start with a regular UIViewController and drag a container view (next to normal UIView in the object list) into its view. Resize it however you want -- the storyboard will automatically provide a view controller connected to this container view with an embed segue. Delete that controller, drag out a table view controller and right-drag from the container view to this table view controller to make a new embed segue. This table view controller can be accessed from the UIViewController with its childViewControllers property (and conversely, you can access the UIViewController from the table view controller with parentViewController if you need to).

如果你不希望你的静态单元格表​​格视图占据整个屏幕,那么使用容器视图是最简单的方法。从一个普通的 UIViewController 开始,将一个容器视图(在对象列表中的普通 UIView 旁边)拖到它的视图中。根据您的需要调整它的大小——故事板将自动提供一个连接到这个容器视图的视图控制器,并带有嵌入转场。删除那个控制器,拖出一个表视图控制器,然后从容器视图中右键拖拽到这个表视图控制器来创建一个新的嵌入转场。这个表视图控制器可以通过它的 childViewControllers 属性从 UIViewController 访问(相反,如果需要,你可以从带有 parentViewController 的表视图控制器访问 UIViewController)。

回答by Jordan Bondo

I'll add to this, since the question is about how to change a UIViewController into a UITableViewController, and given that this question is over a year old and the original answer, while valid and may or may not have been the only solution at the time, doesn't actually answer the question and is not the only solution.

我会补充一点,因为问题是关于如何将 UIViewController 更改为 UITableViewController,并且考虑到这个问题已经一年多了,原始答案虽然有效并且可能是也可能不是唯一的解决方案时间,实际上并没有回答问题,也不是唯一的解决方案。

It IS possible to do this, you just have to set up the table view delegate and datasource outlets in IB, and manually edit the storyboard XML, which sounds scary but is actually very easy.

这是可能的,你只需要在IB中设置表视图委托和数据源出口,并手动编辑故事板XML,这听起来很可怕但实际上很容易。

First, change your class's parent to be a UITableViewController. UITableViewController already adopts the UITableViewDatasource and UITableViewDelegate protocols, so if your class does too you can remove them:

首先,将类的父级更改为 UITableViewController。UITableViewController 已经采用了 UITableViewDatasource 和 UITableViewDelegate 协议,所以如果你的类也采用了,你可以删除它们:

@implementation MyTableViewController : UITableViewController
...
@end

Next, create new referencing outlets on your UITableView for its dataSource and delegate. The easiest way to do this is to control-drag from the UITableView to itself. The popup will give you the dataSource and delegate options.

接下来,在 UITableView 上为其数据源和委托创建新的引用出口。最简单的方法是从 UITableView 控制拖动到它自身。弹出窗口将为您提供数据源和委托选项。

Lastly, you need to change the storyboard XML. The storyboard file can get pretty big pretty fast. The easiest way to find the scene you are looking for is by setting Storyboard Identifier in the Identity Inspector. To view the XML directly, right click on the storyboard file in the project navigator and select "Open As -> Source Code". Now just search for whatever you set the reuse identifier to earlier. You'll see something similar to this:

最后,您需要更改故事板 XML。故事板文件可以很快变大。找到您正在寻找的场景的最简单方法是在身份检查器中设置 Storyboard Identifier。要直接查看 XML,请在项目导航器中右键单击故事板文件,然后选择“打开为 -> 源代码”。现在只需搜索您之前将重用标识符设置为的任何内容。你会看到类似的东西:

<!-- My Table View Controller -->
<scene sceneID="EuE-XX-cCb">
  <objects>
    <viewController storyboardIdentifier="MY_TABLE_VIEW_IDENTIFIER" ... >
      // Lots of other stuff
    </viewController>
  </objects>
</scene>

All you need to do is change the opening and closing view controller tags

您需要做的就是更改打开和关闭视图控制器标签

<viewController>
</viewController>

to be tableViewController instead

改为 tableViewController

<tableViewController>
</tableViewController>

That's it! No need to create a new UITableViewController scene or embed a UITableViewController in a container view.

就是这样!无需创建新的 UITableViewController 场景或在容器视图中嵌入 UITableViewController。

EDIT:

编辑:

I should also add that the UITableView MUST be the root view. It cannot be embedded inside another UIView.

我还应该补充一点, UITableView 必须是根视图。它不能嵌入到另一个 UIView 中。

回答by mxch

What I did, is creating a UITableViewController in IB, open the Storyboard with a text editor, and copy all the nodes inside from the UIViewController to the UITableViewController.

我所做的是在 IB 中创建一个 UITableViewController,使用文本编辑器打开 Storyboard,然后将 UIViewController 中的所有节点复制到 UITableViewController。

I think that with this way there's less risk of deleting something important.

我认为通过这种方式删除重要内容的风险较小。

Before copying the sections objects, make sure that both tableviews (UIViewController and UITableViewController) have the same properties set like: static or dynamic cells, style (plain or grouped), etc.

在复制部分对象之前,请确保两个表视图(UIViewController 和 UITableViewController)具有相同的属性集,例如:静态或动态单元格、样式(普通或分组)等。