如何销毁(解除分配)从 nib 文件加载的视图项的实例

发布于 2025-01-05 02:20:45 字数 1114 浏览 2 评论 0原文

问题:

  • 每次我打开笔尖时,笔尖中的项目的新实例都会出现 创建后,有没有办法销毁(释放内存)它们?
  • 或者不可能取消分配这些视图并且直到应用程序结束为止?

    询问原因: 我在单独的 nib 文件中有 2 个需要加载的视图,因为即使删除了对视图的所有强引用,我也无法销毁 nib 视图项

尝试的步骤(没有成功):

  • 设置视图控制器的视图出口为零
  • 窗口控制器具有视图控制器强大的属性,我已经设置 到 nil

我的理解(基于一些测试)

  • 当我通过代码(使用 alloc)创建相同类型的视图而不加载 nib 文件(NSBundle loadNibNamed)时,我能够销毁视图实例。
  • 但是当由于加载 nib 文件而创建视图时,尽管我已经删除了对该视图的所有强引用,但该视图仍然存在

请注意:它是一个 mac cocoa 项目并使用 自动引用计数 (ARC)

我的项目详细信息:

  • 我有 3 个 nib 文件:
    • MyViewA.nib - 包含 MyViewA 类型的自定义视图
    • MyViewB.nib - 包含 MyViewB 类型的自定义视图
    • MyWindow.nib - 包含一个 NSWindow 和一个自定义视图,我们称之为 作为viewX供参考
  • 有一个窗口控制器和一个视图控制器
  • MyWindow.nib 有 2 个按钮,一个按钮用于将 MyViewA 作为子视图加载到 viewX 和 另一个用于将 MyViewB 作为子视图加载到 viewX
  • 我正在使用 NSBundle 加载 nib 文件
  • 当我尝试向 viewX 添加新的子视图时,我使用 removeFromSuperView 删除已经存在的子视图,并删除对该视图的所有强

引用就像我使用完它们后要删除的 nib 文件项(销毁内存),但不知道如何实现它。

Questions:

  • Every time i open a nib, new instances of the items in the nib are
    created, is there a way to destroy (deallocate memory) them ?
  • Or is not possible to deallocate those views and will till the end of the application ?

    Reason for asking:
    I have 2 views in separate nib files which need to be loaded and since I am unable to destroy the nib view items even after removing all strong references to the view

Steps tried (without success):

  • setting the view controller's view outlet to nil
  • window controller has a view controller strong property, i have set
    that to nil

My understanding (based on some testing)

  • When i create the same type of view through code (using alloc) without loading the nib file (NSBundle loadNibNamed) I am able to destroy the view instance.
  • but when the view gets created as a result of loading the nib file, the view stays on though i have removed all strong references to the view

Pls note: it is a mac cocoa project and uses Automatic Reference Counting (ARC)

My project details:

  • I have 3 nib files:
    • MyViewA.nib - contains a custom view of the type MyViewA
    • MyViewB.nib - contains a custom view of the type MyViewB
    • MyWindow.nib - contains a NSWindow and a custom view, lets call this
      as viewX for reference
  • There is one window controller and one view controller
  • MyWindow.nib has 2 buttons, one button to load MyViewA as a subview to viewX and
    the other for MyViewB to be loaded as a subview to viewX
  • I am loading the nib files using NSBundle
  • When I try to add a new subview to viewX, i remove the already existing subview by using removeFromSuperView and remove all strong references to the view

I would like nib file items to be removed (destroy memory) after I finish using them but am not sure how to achieve it.

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

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

发布评论

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

评论(1

三寸金莲 2025-01-12 02:20:45

我认为既然您使用 NSBundle 手动加载笔尖,您需要手动释放视图。由于您使用的是 ARC,因此无法直接调用release,但是您可以将其转换为 void * 并使用 CFRelease,如下所示:

CFRelease((__bridge void *)viewToRelease);

I think since you loaded the nib manually using NSBundle you need to manually release the view. Since you are using ARC you can't directly call release, however you can cast it to a void * and use CFRelease as follows:

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