问题
I hope this is a simple question. I need to intercept didAddSubview
, but will I need to subclass the UIView
in order to override that method?
The UIView
that I want to override is the UIViewController
's view
property, so I just want to know how to go about working with this.
Thanks!
回答1:
From Apple UIView documentation (see Methods to Override):
When subclassing UIView, there are only a handful of methods you should override and many methods that you might override depending on your needs. Because UIView is a highly configurable class, there are also many ways to implement sophisticated view behaviors without overriding custom methods, which are discussed in the Alternatives to Subclassing section. In the meantime, the following list includes the methods you might consider overriding in your UIView subclasses:
- ...
didAddSubview:
,willRemoveSubview:
- Implement these methods as needed to track the additions and removals of subviews.- ...
So, create your UIView
subclass and override the method. Then, say to your UIViewController
that its view
will be the one you have subclassed. To achieve this there are a couple of ways:
- by Xib
- implementing
loadView
method and set theview
property to be your custom view - redifining the
view
property inviewDidLoad
Hope that helps.
来源:https://stackoverflow.com/questions/11364646/intercepting-didaddsubview