ios 无法在 Interface Builder 中连接 IBOutlet

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

Can't connect IBOutlet in Interface Builder

iosiphoneinterface-builder

提问by haider

I'm having a weird experience. I create any type of iPhone application, add a UIViewController with Xib file. I can edit the xib file with controls and see them rendered if I run. Now i'm trying to add a few IBOutlets to the controller, so I add them to the .h file, synthesize on the .m file. Then i head over to Interface Builder to connect the outlets. I drag the UILabel to the UIView, and then i try to connect the File Owner (which my custom UIViewController), but all that i can select when i try to connect to the UILabel is "view", which is obviosly wrong. So I'm not able to connect IBOutlets to their controls in Interface Builder.

我有一个奇怪的经历。我创建了任何类型的 iPhone 应用程序,添加一个带有 Xib 文件的 UIViewController。我可以编辑带有控件的 xib 文件,并在运行时查看它们的呈现。现在我正在尝试向控制器添加一些 IBOutlets,所以我将它们添加到 .h 文件中,在 .m 文件上进行合成。然后我前往 Interface Builder 连接插座。我将 UILabel 拖到 UIView,然后我尝试连接文件所有者(这是我的自定义 UIViewController),但是当我尝试连接到 UILabel 时我可以选择的所有内容都是“视图”,这显然是错误的。所以我无法将 IBOutlets 连接到它们在 Interface Builder 中的控件。

Any ideas?

有任何想法吗?

回答by Kevin Fisher

I did a combination of the things in this thread and I finally had success.

我在这个线程中做了一些事情,我终于成功了。

In Interface builder (not xcode)

在界面构建器中(不是 xcode)

  1. File->Reload All Class Files
  2. File->Read Class Files (select MyClass.h)
  3. Reconnect File's Owner by
    a. Setting the Class to "MyClass"
    b. Reconnecting the View to the File's Owner's View
  1. 文件->重新加载所有类文件
  2. 文件->读取类文件(选择 MyClass.h)
  3. 通过
    a重新连接文件的所有者。将班级设置为“MyClass”
    b. 将视图重新连接到文件所有者的视图

Everything is back to normal now. Weird.

现在一切都恢复正常了。奇怪的。

Hope this helps more than it confuses ;-)

希望这比混淆更有用;-)

回答by Suiz Uzcategui

Had the same problem.

有同样的问题。

Fixed it by double checking my File's Owner "Class" attribute on the Identity inspector.

通过在身份检查器上仔细检查我的文件所有者“类”属性来修复它。

回答by diatrevolo

In Interface Builder File->Read Class Files, and point it to your File's Owner's class. That fixed it for me.

在 Interface Builder File->Read Class Files 中,并将其指向文件所有者的类。那为我修好了。

回答by Ben S

Are you sure the File's Owner is correctly set to your custom UIViewController and that you've savedyour .hfile after having added the IBOutletkeyword to the instance variable andproperty?

您确定 File's Owner 正确设置为您的自定义 UIViewController 并且您在将关键字添加到实例变量属性后保存了您的.h文件吗?IBOutlet

回答by chepukha

I had pretty much the same problem as you did. I don't know if you solved your problem yet but mine was that I changed all the classes' names, including the viewcontroller, but the file's owner's name remain unchanged. So, I double clicked File's owner -> click i in the window opened -> change the name in the Class field to the new viewcontroller name. Then it worked. Maybe you want to try it again and see if it works. Good luck.

我和你遇到的问题几乎一样。我不知道你是否解决了你的问题,但我的问题是我更改了所有类的名称,包括视图控制器,但文件的所有者名称保持不变。因此,我双击文件的所有者 -> 在打开的窗口中单击 i -> 将类字段中的名称更改为新的视图控制器名称。然后它起作用了。也许你想再试一次,看看它是否有效。祝你好运。

回答by Michael

Did you make sure you are defining your outlets in your .h file as the appropriate type? If you are trying to connect a text field in IB, then your .h file should look something like...

您是否确保在 .h 文件中将出口定义为适当的类型?如果您尝试在 IB 中连接文本字段,那么您的 .h 文件应该类似于...

IBOutlet UITextField    *MyTextField;

Make sure the type is UITextField (or UITextView, etc). If you type them as NSString or anything the is not appropriate, then you can't connect them in Interface Builder....

确保类型为 UITextField(或 UITextView 等)。如果您将它们键入为 NSString 或任何不合适的内容,那么您将无法在 Interface Builder 中连接它们....

回答by Luiza Charmberg

If you failed to connect your UI controls to code via Right-click (or CTRL) -> dropped down list, there is another way. In interface builder, press & hold CTRL, drug mouse from "File's Owner" in the Main window of Interface Builder to your UI control (label, button, etc). The dpopdown list with all defined by you Outlets will be appered. Just choice needed one!

如果您无法通过右键单击(或 CTRL)-> 下拉列表将 UI 控件连接到代码,还有另一种方法。在界面生成器中,按住 CTRL,将鼠标从界面生成器主窗口中的“文件所有者”移至您的 UI 控件(标签、按钮等)。将显示由您定义的所有 Outlets 的 dpopdown 列表。只需要一个选择!

回答by Adrian

I had the same problem for the LONGEST. Changing my File's Owner "Type" to "MyClassViewController" instead of "UIViewController" in the Class identity window fixed my issue. It makes sense because as the File's owner type is UIViewController it's only going to show those properties specific to the base class. Changing the type to MyClassViewController gives access to it's outlets and all inherited instances. Hope this helps!

我在 LONGEST 中遇到了同样的问题。在类标识窗口中将我的文件所有者“类型”更改为“MyClassViewController”而不是“UIViewController”修复了我的问题。这是有道理的,因为 File 的所有者类型是 UIViewController 它只会显示特定于基类的那些属性。将类型更改为 MyClassViewController 可以访问它的插座和所有继承的实例。希望这可以帮助!

回答by Mr H

File->Read Class Files (select MyClass.h)Did the job for me.

File->Read Class Files (select MyClass.h)为我做了这项工作。

回答by Michael

XCode -> Product -> Clean

XCode -> Product -> Clean

Worked for me.

为我工作。