xcode 创建应用程序时如何解决“静态表视图仅在嵌入 UITableViewController 实例时才有效”?

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

How do I resolve "Static table views are only valid when embedded in UITableViewController instances" when creating an app?

iosobjective-cxcodeuitableview

提问by Kartik

I am an Objective-C beginner and I am going through the tutorial to create an IOS app using Apple developer articles.

我是一名 Objective-C 初学者,我正在学习使用 Apple 开发人员文章创建 IOS 应用程序的教程。

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html#//apple_ref/doc/uid/TP40011343-CH8-SW1

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html#//apple_ref/doc/uid/TP40011343-CH8-SW1

I have created an unwind segue and I have gotten stuck. I have gone through SO posts such as given below

我创建了一个放松转场,但我被卡住了。我已经浏览过如下所示的 SO 帖子

  1. StoryBoard issue in Xcode 6.1
  2. Change a UIViewController to a UITableViewController inside a storyboard?
  3. Want to create a cool static UI but : "Static table views are only valid..."
  1. Xcode 6.1 中的 StoryBoard 问题
  2. 将 UIViewController 更改为故事板内的 UITableViewController?
  3. 想要创建一个很酷的静态 UI 但:“静态表视图仅有效......”

I tried to modify the story board source to use "tableViewController" instead of "viewController", but the storyboard won't open.

我尝试修改故事板源以使用“tableViewController”而不是“viewController”,但故事板无法打开。

I am sure that there is an easy solution, but I don't know enough Objective-C or IOS development to know what it is, or how to implement it.

我确信有一个简单的解决方案,但我对 Objective-C 或 IOS 开发的了解不够,无法知道它是什么,或者如何实现它。

I have my controller implementing UITableViewControllerand my view as UITableView. I have attached the screenshot below.

我有我的控制器实现UITableViewController,我的视图为 UITableView。我附上了下面的截图。

XCode Storyboard setup

XCode 故事板设置

and the error message:

和错误信息:

Error message and description

错误信息和说明

My source for ToDoListTableViewController.his given below:

我的来源ToDoListTableViewController.h如下:

 #import <UIKit/UIKit.h>

@interface ToDoListTableViewController : UITableViewController

- (IBAction)unwindToList:(UIStoryboardSegue *)segue;

@end

and the implementation

和实施

#import "ToDoListTableViewController.h"

@interface ToDoListTableViewController ()

@end

@implementation ToDoListTableViewController

 . . . Other methods 

- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
}
@end

回答by Duncan C

Your picture is kind of small so it's a bit hard to tell what we're looking at. Plus your descriptions are a bit vague.

你的照片有点小,所以很难说出我们在看什么。另外你的描述有点模糊。

The deal is that in order to set up a table view as a static table view, it must be managed by a UITableViewController, not a regular UIViewController.

交易是为了将 table view 设置为静态 table view,它必须由 UITableViewController 管理,而不是常规的 UIViewController。

A UITableViewController is a special subclass of UIViewController. When you go to add a new scene to your storyboard, you go to the list of UI objects, find the UITableViewController, and drag one onto the storyboard.

UITableViewController 是 UIViewController 的一个特殊子类。当您要向情节提要中添加新场景时,请转到 UI 对象列表,找到 UITableViewController,然后将一个拖到情节提要上。

An annoying thing about UITableViewController objects is that the ONLY thing they manage is a table view. You can't use them to set up labels, buttons, and other UI elements. Just a table view and nothing else.

UITableViewController 对象的一个​​令人讨厌的事情是它们管理的唯一事物是表视图。您不能使用它们来设置标签、按钮和其他 UI 元素。只是一个表格视图,没有别的。

You said:

你说:

'I tried to modify the story board source to use "tableViewController" instead of "viewController"...'

'我试图修改故事板源以使用“tableViewController”而不是“viewController”......'

I have no idea what that means. What is a "story board source"? You don't "Modify the storyboard source", you drag a UITableViewController onto your storyboard.

我不知道那是什么意思。什么是“故事板来源”?您没有“修改故事板源”,而是将 UITableViewController 拖到故事板上。

Then you said "...but the storyboard won't open." I also don't know what thatmeans. You're going to have to explain that.

然后你说“......但故事板不会打开。” 我也不知道是什么手段。你将不得不解释这一点。

Luckily, there is an easy solution to this.

幸运的是,有一个简单的解决方案。

What you want to do is to create a regular UIViewController to manage everything but the table view, and then put a "Container View" in that view controller and set up an "embed segue" that installs a UITableViewController inside that container view.

您想要做的是创建一个常规 UIViewController 来管理除表视图之外的所有内容,然后在该视图控制器中放置一个“容器视图”并设置一个“嵌入转场”,在该容器视图中安装一个 UITableViewController。

Here's how you do that:

这是你如何做到的:

Search in the list of UI elements on the right side for "container". Drag a container view onto your view controller where you want your table view to appear. Then drag a UITableViewController onto a blank space on your storyboard to create a new storyboard scene. Then control-drag from the container view in your first view controller onto the UITableViewController. This creates an embed segue, which causes the UITableViewController to be loaded as a child view controller with its view inside and sized to fit in the container view.

在右侧的 UI 元素列表中搜索“容器”。将容器视图拖到您希望表视图出现的视图控制器上。然后将 UITableViewController 拖到故事板上的空白区域以创建新的故事板场景。然后从第一个视图控制器中的容器视图控制拖动到 UITableViewController。这会创建一个嵌入转场,这会导致 UITableViewController 被加载为子视图控制器,其视图在内部并调整大小以适合容器视图。

Now you can have a window that has both a table view managed by a UITableViewController AND other contents.

现在,您可以拥有一个窗口,该窗口既有由 UITableViewController 管理的表格视图,也有其他内容。

There are more details to setting this up that are beyond the scope of a SO post. I suggest you do some googling on container views and embed segues and try to find a tutorial on setting them up.

设置此内容的更多详细信息超出了 SO 帖子的范围。我建议您对容器视图进行一些谷歌搜索并嵌入 segue,并尝试找到有关设置它们的教程。