简体   繁体   English

Interface Builder:无法将自定义类分配给表视图控制器

[英]Interface Builder : cannot assign custom class to table view controller

I am trying to create a simple app for iOS that uses a tab bar controller to present either a map view or a table view. 我正在尝试为iOS创建一个简单的应用,该应用使用标签栏控制器来呈现地图视图或表格视图。 Both the map view and the table view have the same navigation bar button items: 地图视图和表格视图都具有相同的导航栏按钮项:

A Logout button on the left, and Refresh, Add New buttons on the right side of the navigation bar. 左侧的注销按钮,以及导航栏右侧的刷新,添加新按钮。 So I have started experimenting the idea of having a parent controller called LocationViewController , which inherits from the UIViewController . 因此,我开始尝试使用名为LocationViewController的父控制器的想法,该父控制器继承自UIViewController

For the table view, I have a controller called TableViewController: LocationViewController , and another controller called MapViewController: LocationViewController . 对于表视图,我有一个名为TableViewController: LocationViewController的控制器TableViewController: LocationViewController和另一个名为MapViewController: LocationViewController控制器MapViewController: LocationViewController

On the storyboard, I added a set of two buttons (for refresh and add new) to the top header section of each view (very small, may be hard to see). 在情节提要上,我在每个视图的顶部标题部分添加了一组两个按钮(用于刷新和添加新按钮)(很小,可能很难看到)。 See the image attached below: 请参阅下面的图片:

MapView

表格检视

There is no problem for me to assign the custom view controller MapViewController to my map view panel, but I cannot find the custom controller class TableViewController in the panel (see the below screenshot) 将自定义视图控制器MapViewController分配给我的地图视图面板没有问题,但是在面板中找不到自定义控制器类TableViewController (请参见下面的屏幕截图)

找不到自定义表视图控制器类

I found a similar post on stackoverflow, and a reboot solved that problem, which I also tried, but did not solve my problem at all. 我在stackoverflow上找到了类似的帖子,重新启动解决了该问题,我也尝试过,但根本没有解决我的问题。

You have added youe buttons in the wrong place. 您在错误的位置添加了您的按钮。 I do not know the right name for the bar, but it contains "associated objects". 我不知道酒吧的正确名称,但其中包含“关联对象”。 What you want to achieve should look something like this : 您想要实现的目标应如下所示: 在此处输入图片说明 Unfortunatelly, you cannot add two UIBarButtonItems to one side of navigation bar via interface builder. 不幸的是,您不能通过界面构建​​器将两个 UIBarButtonItems添加到导航栏的一侧。 You can either drag a UIView instead of UIBarButtonItem to that "slot" and add two UIButtons there, or you ca add multiple buttons from code. 您可以将 UIView而不是 UIBarButtonItem该“插槽”并在其中添加两个 UIButtons ,或者可以从代码中添加多个按钮。


Also, it seems that the name of your question is a bit misleading :) After carefull reread of your post I found the "right" question. 另外,似乎您的问题的名称有点误导:)在仔细阅读您的帖子之后,我发现了“正确”的问题。

Answer - you have added a UITableViewController in your storyboard, but your class doesn't inherit from UITableViewController . 答案-您已在情节UITableViewController中添加了UITableViewController ,但您的类没有继承自UITableViewController If you want to keep the base class, you will have to make it a "plain" UIViewController and add a UITableView to it. 如果要保留基类,则必须使其成为“普通” UIViewController ,并向其添加UITableView Note, that this way you won't be able to have prototype cells. 请注意,这样一来,您将无法拥有原型单元。

You may need to subclass it from UITableViewController. 您可能需要从UITableViewController对其进行子类化。 Replace the UIViewController parent class for UITableViewController, like this: 替换UITableViewController的UIViewController父类,如下所示:

class ViewController: UITableViewController {

Then make sure you selected the View Controller where blue color is all around it and then you can place the name of the class that inherits from UITableViewController. 然后确保选择了周围都是蓝色的View Controller,然后可以放置从UITableViewController继承的类的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM