问题
I have an iOS app that uses a few viewcontrollers with lots of elements. I built the viewcontrollers in the interface builder (in a storyboard). For some key elements (loginbutton, important text, etc..) I created an IBOutlet to be able to use these elements in my code (adjust text/graphics mostly).
For some others where I just needed a quick fix (for example an activity indicator that I show and hide programmatically), I chose to not create an IBOutlet, but instead give them a tag number and access them by the viewWithTag:(NSInteger)
method.
This seemed to work for me (it's only my second iOS project so it might be completely wrong to do this). But now that the viewcontrollers get more complex I find it hard to remember which numbers I already used for tags. Is there any way to keep track of this other than by going trough all the elements and checking their tag?
Maybe it's all wrong to use tags this way, in that case that's fine too. I could look them all up once and just create IBOutlets for all the cases where I use a tag now.
回答1:
I would try to avoid using view tags
as much as possible.
There are always other (and almost always better) ways to do exactly the same thing by using properties
and data models
instead of tags.
Here is an article that I found with some nice examples of why they're not a good idea to use... http://doing-it-wrong.mikeweller.com/2012/08/youre-doing-it-wrong-4-uiview.html
If you need to access a UI element then create an IBOutlet
for it in the interface of the class that needs it.
Alternatively, if there are several elements that are the same then you can create and IBOutletCollection
which is just an array that contains all the elements linked to it.
来源:https://stackoverflow.com/questions/21304553/how-do-you-manage-view-tags-in-your-ios-project