问题描述
这主要是一个风格的问题,但我一直好奇别人的想法是从我开始为iPhone编程。当你的iPhone应用程序中有一个UIView,并且你需要访问它在你的应用程序的其他地方(通常在视图控制器中的另一个函数),你喜欢用一个整数标记视图,并检索它与 viewWithTag:
消息,或者你通常将它设置为视图控制器中的一个属性,以便以后轻松访问?
This is mostly a stylistic question but I've been curious what others' thoughts are since I started programming for the iPhone. When you have a UIView in your iPhone application and you need to access it elsewhere in your application (generally in another function in a view controller), do you like to tag the view with an integer and retrieve it with the viewWithTag:
message, or do you generally set it to a property in the view controller for easy access later?
将其保存为属性显然使得以后更容易检索,但我认为通过标记视图而不是将其设置为对象属性来保存一些(可能是可忽略的)内存量。
Saving it as a property obviously makes it easier to retrieve later, but I would think there is some (perhaps negligible) amount of memory that is saved by tagging a view instead of setting it as an object property.
我一般在我的视图控制器上创建属性,主要是因为我很懒,并且用 viewWithTag:
检索视图很烦人。
I've generally been creating properties on my view controllers, mainly because I'm lazy, and retrieving views with viewWithTag:
is annoying.
推荐答案
没有使用属性来保存内存 - 属性只生成一小段代码,引用一个实例变量指向你的
There is no memory to be saved by not using properties - a property is only generating a small bit of code that refers to an instance variable pointing to your view, that is going to be retained if you point to it or not.
使用viewWithTag将总是更昂贵和更慢,因为该调用必须通过视图层次结构要求每个视图什么是标签值。
Using viewWithTag would always be more expensive and slower, because that call has to go through the view hierarchy asking every view what the tag value is.
我总是使用IBOutlet实例变量,并且有时在我不需要做任何事情的地方添加标签,除了告诉哪个特定的控件称为可以激活的委托方法通过几个不同的控件。这有点不太高效,但代码在这种情况下更容易维护。
I always use IBOutlet instance variables, and add tags to controls sometimes where I don't need to do anything except tell which particular control called a delegate method that could be activated by a few different controls. It's a little less efficient but the code is easier to maintain in that case.
这篇关于你标记你的UIViews或保留它们作为属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!