在 AppDelegate.swift 中将 NSTextField 对象连接到 IBOutlet

Connecting an NSTextField object to an IBOutlet in AppDelegate.swift

最近一直在努力学习Swift,所以如果这个问题看起来比较简单还请见谅

出于某种原因,在选项卡视图控制器中按住 Control 键单击 NSTextField 对象并拖动并没有给我 "insert outlet or action" 的选项,而是 "connect binding" 当我滚动兼容的对象时(在本例中是超类声明)。

为什么我无法插入插座或动作,但可以连接绑定?

附带问题:什么是绑定?

出口只能是您的 nib 文件/故事板场景所有者的 属性。在大多数情况下,哪个是您的视图控制器。

这就是您的视图控制器从情节提要中实例化的结果:

  1. Instantiates views using the information in your storyboard file.
  2. Connects all outlets and actions.
  3. Assigns the root view to the view controller’s view property.
  4. Calls the view controller’s awakeFromNib method.
    When this method is called, the view controller’s trait collection is empty and views may not be in their final positions.
  5. Calls the view controller’s viewDidLoad method. Use this method to add or remove views, modify layout constraints, and load data for your views.

简短版本:

UIKit 为您实例化视图控制器,并根据需要在故事板中添加所有子视图。并将子视图连接(绑定)到它们的出口(如果你创建了一个)。

如何解决你的问题

当您打开助理编辑器时。选择自动 viewController,如果这不是您创建的东西,您应该创建您需要的 viewController 的子 class(在本例中为 UITabViewController)并更改场景的 class 到那个。

当您控制拖动 NSTextField 到控制器时,您需要在故事板中有相同的视图控制器

编辑: 正如 Leo 在评论中提到的,如果您使用故事板,您不能 connect/create AppDelegate 文件中的任何 IBOutlet,您必须在其中创建您放置 NSTextField

的特定控制器

故事板
如图所示,如果您在故事板中,请从那里单击控制器上的顶部栏,您将获得如图所示的选项,select Automatic
如果您在情节提要中 select 编辑了正确的视图控制器,它应该通过 select 您应该能够控制的文件在自动(viewController.swift)中显示相同的 viewcontroller 文件-拖动 IBOutlet

XIB
在 Xib 中,当您 select Xib 并单击 'Asssistant Editor' 它通常会进入正确的视图控制器,如果不是 select 自动也如 Storyboard 图像所示,您将能够连接你 IBOultet

绑定
当您控制拖动 IBOutlet 时,它会在您的控件和视图控制器

的 属性 之间创建一个连接

来自 Apple 文档

The Cocoa bindings feature enables you to establish a live connection between an item of data and its presentation in a view. Any change made to the data’s value, either in the view or in the object that stores the data, automatically propagates across the connection.

您可以在 Apple Doc

中找到更多相关信息