使用 viewWithTag: 的全局声明和在 viewDidLoad 中声明:
我开始了一个新项目,我想知道是否有人尝试过哪种方法更有效,在 .h 文件中声明一个对象(例如 UIButton),或者在 viewDidLoad 方法中创建对象并使用 viewWithTag: 方法来访问创建后的元素。
当然,在 .h 文件中声明它是最简单的,但由于我的应用程序在媒体方面非常繁重(大量图像),我想知道使用标签是否会更好。我所说的更好是指应用程序将使用更少的内存、运行更流畅等等。
感谢您的意见。
I started a new project and I was wondering if anyone has experimented which is more efficient, declaring an object (a UIButton for example) in the .h file or to create the object in the viewDidLoad method and using the viewWithTag: method to access the element after it has been created.
Of course declaring it in the .h file would be the easiest but since my application will be heavy media-wise (lots of images) I was wondering if using tags would be better. By better I mean, that the application would use less memory, run smoother or such things of the like.
Thanks for your input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将使用 IBOutlet 访问 XIB 中声明的 UIView。我建议的是:
1)如果有很多事情发生,但你知道它们有多少(UILabels、UIButtons、UIViews、UIScrollViews 等),我会在 XIB 中创建它们,然后在 IBOulet 中创建它们,这样我就可以控制它们在代码中。它使您的生活变得更轻松(从内存管理的角度来看)。
2)如果您不确定需要多少个视图,我会动态创建它们。因为它让您可以自由添加想要的数量。
我的规则是:如果我知道我将拥有多少个视图,可能会在 XIB 中创建它们(如果不是动态创建的话)。
I would use IBOutlet to access your UIViews declared in the XIB. What i would recommend is:
1) If there is alot of things going on, but you know how many they are (UILabels, UIButtons, UIViews, UIScrollViews, etc) I would create them in the XIB and then IBOulets so I could control them in the code. And it makes life easier for you (on a memory management perspective).
2) If you are not sure how many Views you will need, I would create them dynamically. Because it gives you freedom to add how many do you want.
My rule is: If I know how many views I will have, probably will create them in the XIB, if not dynamically.
现在我回想起来这个问题似乎是一个愚蠢的问题。两者应该具有相同的内存使用量,因为它们都将分配相同的内存量。话虽这么说,对存储在 .h 文件中的对象的引用应该更好。应该会更好,因为 viewWithTag 函数将进行一些处理以获取指向对象的指针,而其他方式它已经存在于您的类中。
Now that I look back on this question it seems like a silly question. The two should be the same memory usage since they both will allocate the same amount of memory. That being said having the reference to the object stored in the .h file should be better. It should be better since the viewWithTag function will do some processing to get the pointer to the object while the other way it will already be there in your class.