NSArraycontroller 访问内容
我正在 Interface Builder 中将数组控制器 (myArraycontroller) 连接到由字典组成的数组 (fileDictionariesArray)。这工作正常,但是当我尝试访问和枚举 arraycontroller [myArrayControllerarrangedObjects]
的内容时,我的内容为零,直到我以编程方式将内容添加到我的 arrayController 中,如下所示
[myArrayController addObjects:fileDictionariesArray];
:我可以循环遍历所有内容。问题是数组(fileDictionariesArray)被添加了两次。一次通过 IB 绑定,一次通过 addObjects。在以编程方式设置内容之前,我无法访问 arrayContoller。我可以删除 arrayController 的内容,然后再次以编程方式设置它,如下所示:
[myArrayController setContent:nil];
[myArrayController addObjects:fileDictionariesArray];
这给出了 arrayController 中正确的项目数,但这似乎不是执行此操作的正确方法。我希望任何人都可以给我一个关于如何通过arrangedObjects 数组访问我的arrayController 的提示,而无需添加内容两次。
感谢您的帮助。干杯,特隆德·克里斯蒂安森
I am connecting an arraycontroller (myArraycontroller) in Interface Builder to an array (fileDictionariesArray) consisting of dictionaries. This works fine, but when I try to access and enumerate over the contents of the arraycontroller [myArrayController arrangedObjects]
I get nil in content until I add the content programmatically to my arrayController like this:
[myArrayController addObjects:fileDictionariesArray];
After I have done that I can loop over all the contents. The problem is that the array (fileDictionariesArray) has been added two times. One time through IB binding and one time through addObjects. I just can't access the arrayContoller until I set the content programmatically. I can delete the content of the arrayController and then set it programmatically again like this:
[myArrayController setContent:nil];
[myArrayController addObjects:fileDictionariesArray];
Which gives the correct number of items in the arrayController, but it does not seem like the correct way to do this. I would appreciate it of anyone could give me a hint on how to access my arrayController through the arrangedObjects array without adding the content two times.
Thank you for your help. Cheers, Trond Kristiansen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的帖子以及您和巴伐利亚之间的评论,我确信他不应该删除他的答案。他问你在哪里调用这段代码(从 -awakeFromNib?)。这是一个必须回答的重要问题。
如果您从 -awakeFromNib 调用它,下一个问题是“nib 中的哪个对象?所有者?其他控制器?什么是文档?”
问题是,在调用代码之前,数组控制器可能没有时间观察数组的“更改”。这就是为什么强制添加内容会导致其被添加两次(因为完成后,数组控制器会观察其内容数组并拉入内容......再次......此时你已经将数组的内容加倍了) 。
如果您正在执行立即启动的操作,那么最好直接在阵列上进行操作。只要在启动完成后将责任留给阵列控制器,这并没有什么“错误”或“肮脏”的地方。
很难说得更具体,因为我只是猜测你可能在做什么。修改您的问题,以包含更完整的描述,说明您的阵列控制器如何/在何处适合架构,您在启动时对阵列具体做了什么,等等。缺少任何这些信息都会导致太多猜测。
Based on your post and the comments between you and Bavarious, I'm convinced he shouldn't have deleted his answer. He'd asked you where you were calling this code (from -awakeFromNib?). This is an essential question to answer.
If you call this from -awakeFromNib, the next question is "of what object in the nib? the owner? some other controller? what is the? a document?"
The thing is the array controller likely doesn't have time to observe the "changes" to the array before you call your code. That's why adding the content forcibly results in its being added twice (because after you're done, the array controller observes its content array and pulls in the content ... again ... at which point you've doubled the array's contents).
If you're doing immediate startup stuff, it might be best to operate directly on the array. There's nothing "wrong" or "dirty" about that, so long as you leave responsibility with the array controller after startup is complete.
It's hard to get any more specific because I'm just guessing at what you might be doing. Amend your question to include a more complete description of how/where your array controller fits into the architecture, what specifically you're doing to the array at startup, etc. Missing any of this information forces too many guesses.