How do you manage view tags in your iOS project? [closed]

ぐ巨炮叔叔 提交于 2020-01-05 14:03:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!