Xcode 4:创建 UIView xib,未正确连接

发布于 2024-11-24 04:28:05 字数 582 浏览 0 评论 0原文

我正在尝试创建一个笔尖,其中包含将嵌入到 TableViewCell 中的视图。我创建了接口和实现文件 ResultCell.hResultCell.m。它们是现货,开箱即用,无需更改代码。

然后,我创建一个空的 XIB 文件,并将 UIView 拖到其上。然后,我单击“文件所有者”,并将类型设置为 ResultCell。我单击该视图,并将其类也设置为 ResultCell

以下是我遇到的问题:

  1. 使用 Assistant Editor 视图(我所在的位置)时,当我查看 ResultCell.xib 文件时,ResultCell.h 文件不会出现。我必须通过单击“自动”并选择文件来强制加载它。
  2. 当我在视图中放置标签,然后按住 Ctrl 键并单击并拖动到 .h 以创建插座时,我收到以下错误消息:“无法插入新的插座连接:找不到任何插座”有关名为 ResultCell 的类的信息。”

我尝试过创建和重新创建视图,但它不起作用,我开始失去耐心。任何帮助将非常非常感谢!

I'm trying to create a nib that contains a view that will be embedded in a TableViewCell. I've created the interface and implementation files, ResultCell.h and ResultCell.m. They're stock, out-of-the-box, no code changes.

I then create an empty XIB file, and drag a UIView onto it. Then I click File's Owner, and set the type to ResultCell. I click the view, and set its class to also be ResultCell.

Here are the problems I have:

  1. When using the Assistant Editor view (which I live in), the ResultCell.h file doesn't appear when I'm viewing the ResultCell.xib file. I have to force it to load by clicking on Automatic and selecting the file.
  2. When I drop a label in the view, and then Ctrl+Click and drag to the .h to create an outlet, I get this error message: "Could not insert new outlet connection: Could not find any information about the class named ResultCell."

I've tried creating and re-creating the view, and it's just not working, and I've started to lose my patience. Any help would be very, very appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

香橙ぽ 2024-12-01 04:28:06

我刚刚遇到了这个问题,重新启动 Xcode 并没有解决它。我从项目中删除了类文件,然后将它们添加回项目中,然后它开始工作。

I just had this problem and restarting Xcode did not fix it. I removed the class files from the project then added them back in and it started working.

深海夜未眠 2024-12-01 04:28:06

我有同样的问题。我尝试过:

  • 重新启动 Xcode
  • 删除派生数据
  • 还有更多

这些都不起作用。对我有用的就是:

  • 从项目中删除“可疑”类文件(只需删除引用,嗯?)
  • 将它们重新添加到项目中

就完成了!

I had the very same issue. And I tried :

  • Restarting Xcode
  • Deleting Derived Data
  • And many more

None of that worked. What worked for me was simply to :

  • Remove 'suspect' class files from Project (just remove References, huh?)
  • Re-add them to the project

And done!

谁人与我共长歌 2024-12-01 04:28:06

如果重新启动 Xcode 对您不起作用,我发现将新的 .m 文件切换回目标成员身份是可行的。

If restarting Xcode doesn't work of you, I have found that Toggling the new .m file out and back into the target membership works.

梦忆晨望 2024-12-01 04:28:06

这帮助了我:

  1. 从项目文件面板(左侧)中选择损坏的 xib 文件。
  2. 单击编辑器视图中的文件所有者图标。
  3. 从属性面板(右侧)选择第三个选项卡(顶部)
  4. 指定“自定义类”

如果您没有看到“面板”,请查看窗口的右上角并启用正确的“查看”按钮。如果您没有看到“查看”按钮,请单击右上角的胶囊按钮。

This helped me out:

  1. from the project file Panel (left side) select the xib file that is broken.
  2. Click on the Files Owner icon from the editor view.
  3. from the properties Panel (right side) select the third tab (at the top)
  4. specify the "Custom Class"

If you do not see a "panel", please have a look at the top-right corner of the window and enable the proper "view" buttons. If you don't see the "view" buttons then click on the top-right most capsule button.

清醇 2024-12-01 04:28:06

我注意到 .m 文件已移至 en.lproj 文件夹内。

只需从 Xcode 中删除(仅供参考).m 文件并将 .m 从 en.lproj 中移出。再次添加即可解决问题。

不用担心,您将恢复所有连接。

I noticed that .m file was moved inside en.lproj folder.

Just delete (reference only) the .m file from the Xcode and moved .m out of the en.lproj. Add it again.It will fix the issue.

Don't worry, you will get all your connections back.

走过海棠暮 2024-12-01 04:28:06

我今天遇到了同样的问题。重新启动 XCode 并没有解决我的问题。我可以通过使用项目“派生数据”“删除”选项使事情恢复正常,该选项可以在<强>组织者。组织者表示,“派生数据包括索引、构建输出和日志”。我猜想索引构建输出导致了这个问题。

I encountered the same problem today. Restarting XCode did not fix the problem for me. I was able to get things back to normal by using "Delete" option of "Derived Data" of the project that can be found under Organizer. Organizer says that, "Derived Data includes index, build output and logs". I guess that either index or build output was causing this issue.

待天淡蓝洁白时 2024-12-01 04:28:06

在 XCode 中,转到管理器,单击项目,单击删除派生数据...然后清理项目

in XCode go to organizer, click project, click delete derived data... than clean the project

聊慰 2024-12-01 04:28:06

就我而言,它有助于对相应的头文件进行“假更改”(只是一个空格)。

In my case it helped to make a "fake change" (just a space) to the according header file.

┊风居住的梦幻卍 2024-12-01 04:28:06
  1. ResultCell 应该是 UITableViewCell 的子类
  2. 您不应该将 UIView 拖动到空画布上,您应该拖动 UITableViewCell
  3. 您应该选择 cell 并将 it 更改为您的子类
  4. 所有者通常是一个具有单元出口的控制器

每次您想要加载新单元时,请调用:

 [[NSBundle mainBundle] loadNibNamed:nibName 
                               owner:controllerWithOutletToCell 
                             options:nil]

加载后,使用该单元,并将 ivar/outlet 设置为nil,为下一次加载做好准备

还有其他方法,但这很常见

  1. ResultCell should be a subclass of UITableViewCell
  2. You should not drag a UIView onto the empty canvas, you should drag a UITableViewCell
  3. You should select the cell and change it to be your subclass
  4. The owner is most often a controller with an outlet to the cell

Everytime you want to load a new cell, call:

 [[NSBundle mainBundle] loadNibNamed:nibName 
                               owner:controllerWithOutletToCell 
                             options:nil]

after loading, use the cell, and set the ivar/outlet to nil, ready for the next load

there are other ways, but this is common

烂柯人 2024-12-01 04:28:06

也许对我有用的是这个..(Xcode v4.5)

这不起作用

我试图控制拖动到我的.h

@interface SearchViewController : UIViewController

@end

< strong>这有效(也许这就是它应该如何工作的,我以前不知道)
请参阅左括号和右括号。控制在闭括号后的拖放。

@interface SearchViewController : UIViewController
{
}

@end

Perhaps what worked for me is this.. (Xcode v4.5)

This did not work

I was trying to control drag into the interface definition of my .h

@interface SearchViewController : UIViewController

@end

This worked (may be that is how it was supposed to work, I did not know it before)
See the open and close brackets. Control drag and drop after the closed bracket.

@interface SearchViewController : UIViewController
{
}

@end
落花浅忆 2024-12-01 04:28:06

我在用 Swift 编写的项目中遇到了类似的问题。

对我有用的是在这样的代码中设置 IBOutlet

@IBOutlet var foo: UIView?

,然后通过拖动到代码行旁边出现的小圆圈将其连接到 Interface Builder。

I had a similar problem with a project written in Swift.

What worked for me was setting up the IBOutlet in code like this

@IBOutlet var foo: UIView?

and afterwards connect it to Interface Builder by dragging to the little circle that appeared right next to the code line.

幸福丶如此 2024-12-01 04:28:06

上述解决方法都没有为我解决连接器问题,因此我搁置了我的宠物项目,直到遇到以下 stackoverflow 线程:

https:// stackoverflow.com/a/15873770/2846800

我一直在以前版本的 XCode 中开发一个大型项目,并关闭了索引。通过重新启用索引,我的问题现已解决:

defaults delete com.apple.dt.XCode IDEIndexDisable

我现在可以使用 Interface Builder 的 D&D 功能。我希望这可以帮助其他人......

None of the above workarounds resolved the connector issue for me so I shelved my pet project until I came across the following stackoverflow thread:

https://stackoverflow.com/a/15873770/2846800

I had been working on a large project in a previous version of XCode and had turned off indexing. By re-enabling indexing my issue is now fixed:

defaults delete com.apple.dt.XCode IDEIndexDisable

I can now use the D&D features of Interface Builder. I hope this can help others...

卷耳 2024-12-01 04:28:06

这个问题似乎是一个 Xcode 错误,当您用同名的新文件替换文件时,该错误通常会出现。 Xcode 似乎保留了同名旧文件的引用。

无论如何,解决此问题的方法似乎是:

  1. 清理项目
  2. 在项目导航器视图中单击(您的项目)。转到您要构建的目标的“构建阶段”下的“编译源”。删除并添加回给您带来麻烦的 .m 文件。
  3. 或者,在 NIB 文件的文件检查器(实用程序视图)中的目标成员身份下,取消选中并重新选中目标名称。
  4. 重新启动 Xcode 以获得更好的效果。
  5. 当然,单独删除并添加回 .m 文件也应该可以解决此问题。

(仅步骤 1 和 2 就为我解决了这个问题。)

This problem seems to be an Xcode bug which creeps up mostly when you replace a file with a new file of the same name. Xcode seems to keep a reference of the older file of the same name.

Anyhow, the way to fix this seems to be:

  1. Clean the project
  2. Click (Your Project) in Project Navigator view. Go to Compile Sources under Build Phases of the target for which you are building. Remove and add back the .m file which is giving you trouble.
  3. Alternatively, in the File Inspector (Utilities view) of the NIB file, under Target Membership, uncheck and check back the target name.
  4. Restart Xcode for good measure.
  5. Of course, deleting and adding back the .m file alone should fix it too.

(Steps 1 and 2 alone fixed it for me.)

从来不烧饼 2024-12-01 04:28:06

如果您从其他项目复制文件,请确保选中“添加到目标”框

If you copied files from other project just make sure you check the 'Add to targets' box

人│生佛魔见 2024-12-01 04:28:06

重命名类文件可能会取消它们与 XIB 的链接。这个答案帮助我发现了这一点:

Ctrl-Drag从按钮到方法不起作用。 Xcode/Interface Builder

检查您的自定义类是否在身份检查器中正确设置。

Renaming the class files may unlink them from the XIB. This answer helped me discover this:

Ctrl-Drag from button to method not working. Xcode/Interface Builder

Check that your custom class is set correctly in the Identity Inspector.

波浪屿的海角声 2024-12-01 04:28:06

症状相似,但原因不同。

显然,当焦点位于助手视图上时,我按了退格键,因为标准文件模板注释行之一从 // 到 / ,这导致文件无法编译。

修复注释允许 SDK 解析文件,将其识别为 UIViewController,并添加到插座中。

SO - 如果您遇到此问题,请进行构建或分析以查看视图控制器 .h 文件中是否存在需要修复的错误。然后尝试其他解决方案。

Similar symptom, but different cause.

Apparently I hit backspace when the focus was on the assistant view, because one of the standard file template comment lines went from // to / which results in the file not compiling.

Fixing the comment allowed the SDK to parse the file, recognize it as a UIViewController, and add in the outlet.

SO -- if you have this problem, do a build or analyze to see if there are errors that need fixing in your view controller .h file. THEN try the other solutions.

空宴 2024-12-01 04:28:06

这可能是一个老话题,但以防万一将来有人遇到同样的问题,请尝试删除关联的 .xib、.h 和 .m 文件并创建新文件。对我来说,.h 文件中的 UIViewController 不是紫色的,甚至退格并键入它也没有帮助。

This might be an old topic but just in case anyone has the same issue in future, try deleting the associated .xib,.h and .m files and create new ones. For me, the UIViewController in my .h file wasn't purple and even backspacing and typing it didn't help.

看轻我的陪伴 2024-12-01 04:28:06

只需在@implementation 和@synthesize 之间添加一条线即可。这里的大多数答案都有一个共同点。这对我有用

@implementation 

@synthesize

Just put a line between the @implementation and the @synthesize. Most of the answers here have that in common. It worked for me

@implementation 

@synthesize
单挑你×的.吻 2024-12-01 04:28:06

我只是尝试了这里的所有内容,但没有任何效果(使用 Xcode 5 的开发者预览版)。

唯一对我有用的就是将这个不完整的行放入我的界面文件中:

@property (nonatomic, weak)

无论出于何种原因,通过从 .xib 拖放添加插座和操作之后效果很好。

I just tried everything here and nothing worked (using a Developer Preview for Xcode 5).

The only thing that worked for me was to put this incomplete line in my interface file:

@property (nonatomic, weak)

For whatever reason, adding outlets and actions by drag and drop from a .xib worked fine after that.

薄凉少年不暖心 2024-12-01 04:28:06

对我来说,清理或删除 DerivedData 都解决了问题。

我多次尝试删除并重新创建 UIViewController 类,但一次又一次遇到问题。

然后我重新创建了 UIViewController 类并给它一个不同的名称。这为我解决了问题。

For me wether cleaning nor deleting the DerivedData solved the problem.

I tried to delete and recreate my UIViewController class several times and got the problem again and again.

Then I recreated the UIViewController class and gave it a different name. This solved the problem for me.

病毒体 2024-12-01 04:28:06

我遇到过这个问题,并找到了上面未列出的解决方案。

就我而言,我可以看到类 .h 文件中有问题,因为我的自定义视图控制器无法识别该类:UIViewController(它是黑色而不是紫色)。所有其他自定义视图控制器都有:紫色的 UIViewController。

就我而言,也可能是您的情况,我需要将类添加到目标/构建阶段/编译源下拉列表中。需要添加 .m。所有其他 .m 都在那里,但没有这个。

添加后,:UIViewController 显示为紫色,一切正常。

I have had this problem and found a solution not listed above.

In my case, I could see there was something wrong in the class .h file because my custom view controller did not recognise the class : UIViewController (it was in black not purple). All the other custom view controllers had the : UIViewController in purple.

in my case, and possibly yours, I needed to add the class to targets/build phases/compile Sources drop down. The .m needs to be added. All of the other .m's were there but not this one.

Once I added it, the :UIViewController appeared in purple and everything worked fine.

巷雨优美回忆 2024-12-01 04:28:06

就我而言,我有多个项目以及一些其他依赖项。我试图通过删除派生数据或重新启动XCode来解决它,但它无法工作。最后,我尝试了以下方法,效果很好:

转到目标> >仅构建目标架构>并将“调试”和“发布”都设置为“否”。

为工作区中的所有项目设置此项并重新编译以确保没有其他错误。也许没有意义,但不知何故做到了。在编译过程中,我遇到了其他问题,例如链接器错误和未找到符号。

For my case, I have multiple project with some other dependencies. I tried to solve it by deleting derived data or restarting XCode but it couldn't work. In the end, I tried the following method and it works:

Go to target > Build Target Architectures Only > and set both Debug and Release to NO.

Set this for all projects in the workspace and recompile to make sure there is no other errors. Maybe not making sense but somehow did it. During my compilation, I had other issues such as linker errors and symbols not found.

七堇年 2024-12-01 04:28:06

对于此类视图的 *.m 文件,打开“显示文件检查器”并取消选中该项目的“目标成员身份”,然后执行清理(菜单产品->清理)并再次检查该项目的“目标成员身份”

在此处输入图像描述

For *.m file of this class view open "Show File Inspector" and unchecking "Target membership" for this project, then do Clean (menu Product->Clean) and checking again "Target membership" for this project

enter image description here

甜心小果奶 2024-12-01 04:28:06

使用 Swift,我遇到了类似的问题。我发现评论对我来说是问题的一部分。

我利用默认视图控制器,在其中做了一些工作,然后通过复制整个第一个视图控制器、将其剥离为仅 viewDidLoad() 并将该类重命名为 TestViewController 来创建第二个视图控制器。构建有效,代码执行。一切都很好。

当我拖入 UITextField 来创建插座时,它不允许我这样做。我注意到我的评论仍然说“ViewController.swift”,所以我将评论文本更改为“TestViewController.swift”并重新构建。我能够连接我的插座!

我想知道索引器是否正在读取注释和类名,并且不知何故感到困惑。

Using Swift, I had a similar problem. I found out that the comments were part of the problem for me.

I leveraged the default view controller, did some work in it, then created a second view controller by copying the entire first one, stripping it down to only viewDidLoad() and renaming the class to TestViewController. Builds worked, code executed. All was good.

When I went to drag a UITextField in to create an outlet, it would not let me. I noticed that my comments still said "ViewController.swift", so I changed the comment text to "TestViewController.swift" and rebuilt. I was able to connect my outlet!

I wonder if what occurred is that the Indexer was reading the comments as well as the class names, and somehow got confused.

情未る 2024-12-01 04:28:06

希望这对那里的人有帮助。

我只需删除派生数据文件夹即可。您需要点击窗口->主办方->项目->删除派生数据

并重新启动 Xcode。

你应该可以走了!

Hope this helps someone out there.

I just had to delete the derived data folder. You need to click Window -> Organizer -> Projects -> Delete Derived Data

AND RESTART XCODE.

You should be good to go!

木緿 2024-12-01 04:28:06

正如其中一些答案所暗示的那样,新类不属于“目标”的一部分可能是造成这种情况的根本原因。

创建新的可可类时,默认情况下会选中将新类添加到目标的选项,但出于任何原因,如果您在单击“保存”时不选中此选项,则会遇到此问题。

New Class Target

将新类添加到应用程序目标的任何解决方法都可以解决该问题,并且是一些所有这些“修复”都有一个共同点。

我认为 XCode 可能存在一些错误,导致新文件默认情况下不会添加到应用程序的主要目标中。修复归结为将“损坏的”类添加到主目标

The new class not being part of the "TARGET" is likely the root cause of this, as alluded to by some of these answers.

When creating a new cocoa class, by default the option to add new class to target is by default checked, but should, for any reason, this not be checked when you hit save, you will have this issue.

New Class Target

Any of these workarounds that add the new class to the apps target will work to resolve the problem, and is something that all these 'fixes' have in common.

I think XCode likely has some bugs that causes a new file to, by default, NOT be added to the apps main target. The fix boils down to adding your 'broken' class to the main TARGET

混浊又暗下来 2024-12-01 04:28:06

在 XCode 7.1 中,在源文件的不同位置添加连接器是有效的。我遇到的错误是引用我的变量之一,就好像它是一个类一样(没有任何意义)。当我将连接器添加到该变量下方而不是上方时,错误停止发生。

In XCode 7.1, adding the connector at a different point in my source file worked. The error I was getting referred to one of my variables as if it were a class (didn't make any sense). The error stopped occurring when I added the connector below that variable instead of above it.

奈何桥上唱咆哮 2024-12-01 04:28:06

这个问题还有一个不同的原因...如果您使用 ios 以外的模板创建了新的可可类文件...我可能会发生...

解决方案是删除它并使用正确的模板重新创建它
输入图片此处描述

There is also a diferent reason for the issue ... if you have created new cocoa class file with template other than ios ... i can happen...

The solution would be to delete it and re create it with right template
enter image description here

纸短情长 2024-12-01 04:28:05

今天我也陷入了类似的状态。这很奇怪:我可以将任何 XIB(新的或现有的)附加到项目中任何已存在的 ViewController 类,但我无法创建新的 ViewController 并将其正确附加到任何 XIB。 “助手”功能不起作用,IB 到头文件连接功能也不起作用。

关闭项目并重新打开并没有解决问题。
退出 XCode 并重新启动并没有解决问题。
创建一个新项目并测试功能运行良好,这让我相信某个缓存中的某些内容已损坏。

我的解决方案

  • 关闭您的项目。
  • 转到 ~/Library/Developer/XCode/DerivedData 文件夹并删除其中引用您正在处理的项目的所有子文件夹。
  • 在 XCode 中打开您的项目。现在问题应该得到解决。

顺便说一句,仅仅进行全面清理似乎并不能解决问题。我不得不丢弃派生数据。我确信我之所以进入这个位置是因为我正在玩 git 游戏,但我不知道如何摆脱,因为即使切换回早期的 git 修订版也没有帮助。 (这也是一个很大的线索,表明它是项目本身未跟踪的东西。)

I got into a similar state just today. It was very odd: I could attach any XIB (new or existing) to any already-existing ViewController class in the project, but I could not create a new ViewController and have it attach properly to any XIB. The "assistant" functionality didn't work, nor did the IB-to-headerfile-connection functionality.

Closing the project and re-opening did not fix it.
Quitting XCode and restarting did not fix it.
Creating a new project and testing the functionality worked fine, which led me to believe there was something corrupt in some cache somewhere.

My solution

  • Close your project.
  • Go to the ~/Library/Developer/XCode/DerivedData folder and REMOVE all subfolders there referencing the project you are working on.
  • Open your project in XCode. The problem should be fixed now.

Incidentally, just running a full clean did not seem to clear things up. I had to trash the derived data. I'm certain I got into this position because of git games I was playing, but I had no idea how to get out, because even switching back to earlier git revisions didn't help. (That was a big clue also that it was something untracked by the project itself.)

紫竹語嫣☆ 2024-12-01 04:28:05

这可能不适用于您的特定问题,但在使用新创建的笔尖时偶尔会遇到该错误。删除并重新创建与以前同名的 nib 和视图控制器并不能解决问题,但重新启动 Xcode 可以解决问题。

This might not work for your specific issue, but occasionally I get that error when working with newly created nibs. Deleting and recreating the nibs and View Controllers with the same names as before didn't resolve the issue, but relaunching Xcode did.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文