How to get a certain subview from UIView by tag

前端 未结 6 1096
野趣味
野趣味 2021-02-03 21:00

I\'m a noob in Objective-C and I have one question.

I have one UILabel object that I adding to one UIView with this code:

UILabel *label = [         


        
6条回答
  •  失恋的感觉
    2021-02-03 21:46

    You can get the subview with the code which others have mentioned, just like

    UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
    

    But an important point to remember,

    • Make sure the parent view doesn't have the same tag value as of the subview. Otherwise the viewWithTag: method will return the receiver view (on which you are calling viewWithTag:) instead of returning the actual subview you want.

    So keep the parent view and child views tags distinct whenever you need to use viewWithTag:.

提交回复
热议问题