ios 无法在 Interface Builder (Xcode 5) 中创建到子视图的插座连接

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

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

iosobjective-cuiviewxcode5

提问by Patricia

I know this appears to be a duplicate of some other questions, but the answers are not working for me.

我知道这似乎是其他一些问题的重复,但答案对我不起作用。

  1. I have created a single view app.
  2. In the storyboard I added a subview to my main view.
  3. I have a label on my main view and another label on my subview.
  4. I have created a class of type UIView and added it as the custom class for the subview.
  5. I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
  6. I have even typed the property information and tried to make the connection manually to no avail.
  1. 我创建了一个单一视图应用程序。
  2. 在故事板中,我在主视图中添加了一个子视图。
  3. 我的主视图上有一个标签,子视图上有另一个标签。
  4. 我创建了一个 UIView 类型的类并将其添加为子视图的自定义类。
  5. 我可以按住 ctrl 键将主视图上的标签拖动到主视图控制器类。但是,当我尝试将子视图上的标签按 ctrl 拖动到自定义类时,我无法建立连接。
  6. 我什至输入了属性信息并尝试手动建立连接无济于事。

Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.

在最新版本的 Xcode 的 Interface Builder 中,情况发生了一些变化。有人可以告诉我我缺少什么吗?这里实际上没有代码。我只是在测试尝试使用自定义类将插座连接到子视图。

The first image shows that I have set up the custom class and added a property but I cannot make the connection.

第一张图片显示我已经设置了自定义类并添加了一个属性,但我无法建立连接。

Main view and subview in Interface Builder - custom class is set

Interface Builder 中的主视图和子视图 - 设置自定义类

The second image shows the main view label is connected in the main view's controller.

第二张图显示主视图标签连接在主视图的控制器中。

Main view and subview in Interface Builder - main view's label is connected

Interface Builder中的主视图和子视图——主视图的标签是相连的

The third image shows that there are no outlet connections for the subview's label.

第三张图显示子视图的标签没有出口连接。

Main view and subview in Interface Builder - cannot connect subview's label

Main view and subview in Interface Builder - cannot connect subview's label

回答by Rob

You can manually write the IBOutletproperty declaration in the @interfaceof the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.

可以在自定义视图子类的 中手动编写IBOutlet属性声明@interface,假设已经在IB中定义了子视图的基类,那么就可以从代码中的outlet圆拖回场景中的控件。

enter image description here

enter image description here

Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.

或者,正如您所指出的,Warren Burton 在他对另一个问题Can't Wire to Subview in IB 的回答中建议了这种技术和另一种技术。

回答by Choppin Broccoli

The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.

该问题与视图控制器的文件所有者有关。它可能设置为 IOViewController,因此您只能在该 .h 文件中进行属性连接。

What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.

您可以做的是为子视图创建另一个 .nib 文件并将子视图放在那里。然后在该 .nib 文件中,将文件所有者设为 IOSubview。属性连接在那里工作得很好。然后只需以编程方式将子视图添加到您的 IOViewController。请记住首先从包中加载 nib 文件。

回答by zevij

This is what I did (in Swift):

这就是我所做的(在 Swift 中):

  • I Created a new ViewController (e.g. class MyViewController: UIViewController {})
  • In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
  • Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
  • 我创建了一个新的 ViewController(例如 class MyViewController: UIViewController {})
  • 在 StoryBoard 中,我展开了“场景”(即所有 UI 组件的树视图)并选择了“MyViewController”
  • 使用“身份检查器”我分配了“MyViewController”类(与默认的 UIViewController 相对)

After that I was able to assign an action.

在那之后,我能够分配一个动作。

I suspect that for Obj-C it is similar process.

我怀疑 Obj-C 是类似的过程。

回答by Josue Espinosa

You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #importthe subclass into IDViewController.h and create an outlet there.

您不在子类中创建插座,而是在它所在的视图控制器上创建插座。您需要#import将子类转换为 IDViewController.h 并在那里创建一个插座。

IDViewController.h

IDViewController.h

#import "IDSubclass.h"
...
@property (strong, nonatomic) IBOutlet IDSubclass *outletName;

回答by Makku

Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

将故事板放大到 100%。如果缩小,例如 50%,则插座连接将不起作用。