简体   繁体   English

Xcode故事板:无法将View添加到ViewController

[英]Xcode Storyboard: Can't add View to ViewController

I'm using Xcode 10 (with Swift 5 and for iOS 12) and my app is currently set up like this: 我正在使用Xcode 10(在Swift 5和iOS 12上),我的应用当前设置如下:

  1. UIViewController 1 (login screen) - segue12 to -> UIViewController 1(登录屏幕)-segue12至->
  2. NavigationController that automatically created 自动创建的NavigationController
  3. UIViewController 2 with a UITableView - segue23 to -> 带有UITableView UIViewController 2-segue23至->
  4. UIViewController 3 (detailed info about an item in the UITableView with automatically created "back" button) UIViewController 3(有关带有自动创建的“后退”按钮的UITableView项的详细信息)

在此处输入图片说明

The NavigationBar only contains a " ". NavigationBar仅包含“”。

I now want to add another UIView with a button below the UITableView but Xcode won't let me drag it below the UITableView , only either into the UITableView or directly underneath "First Responder", which puts the View outside into its own window. 现在,我想在UITableView下方添加另一个带有按钮的UIView ,但是Xcode不允许我将其拖到UITableView下方,只能拖入UITableView或直接在“第一响应者”下方,这会将View置于其自己的窗口之外。

If I delete "My Table View", it won't let me drag in a replacement either. 如果删除“我的表格视图”,它也将不允许我拖动替换。

How do I fix this, so I can add the new UIView + UIButton ? 如何解决此问题,以便添加新的UIView + UIButton

NavigationController that automatically created UIViewController 2 自动创建UIViewController 2的NavigationController

That explains everything. 这说明了一切。 When you drag a navigation controller into the storyboard, what you get is a navigation controller and its root view controller, and the root view controller is a UITableViewController. 将导航控制器拖到情节提要中时,得到的是导航控制器及其根视图控制器,而根视图控制器是UITableViewController。

Let's step back for a moment. 让我们退后一会儿。 A view controller can and must have exactly one main view. 一个视图控制器可以并且必须仅具有一个主视图。 It occupies the whole scene. 它占据了整个场景。 It cannot be resized. 无法调整大小。 It cannot have a sibling view, because it has no superview for a sibling view to be a child of. 它不能具有同级视图,因为它没有父级视图作为同级视图的子级。 It can only have children (subviews). 它只能有子级(子视图)。

Well, in this case, ViewController2 is a UITableViewController, and MyTableView is ViewController2's main view. 好吧,在这种情况下,ViewController2是UITableViewController,而MyTableView是ViewController2的主视图。 (A table view controller is always configured this way, and that is what you got when you dragged the navigation controller into the storyboard.) That is why you cannot add more views to it, except as subviews, eg prototype cell contents. (始终以这种方式配置表视图控制器,这就是将导航控制器拖到情节提要中时所得到的。)这就是为什么您不能向其中添加更多视图的原因,除了作为子视图之外,例如原型单元内容。 Your button has no place to go except inside the table view. 除了表视图内部 ,您的按钮无处可去。

So, what to do? 那么该怎么办?

  • If the extra interface you want to add is just a button, the usual solution is to put it into the navigation bar . 如果您要添加的额外界面只是一个按钮,通常的解决方案是将其放入导航栏中 It becomes part of the table view controller's navigation item. 它成为表视图控制器导航项的一部分。

  • If you don't want to do that — that is, if you really want the button and the table view to be siblings of one another in the interface — then you need to replace ViewController2 itself with an ordinary view controller. 如果您不想这样做-也就是说,如果您确实希望按钮和表视图在界面中彼此相同,则您需要用普通的视图控制器替换ViewController2本身。 Now its main view will be an ordinary view, and you can drag anything you like into it, including a table view and a button. 现在,其主视图将是普通视图,您可以将所需的任何内容拖到其中,包括表格视图和按钮。 More likely, instead of a bare table view, you would use a container view , with an embed segue to a table view controller. 您更有可能使用容器视图 (而不是裸表视图),并将容器 嵌入表视图控制器中。

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

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