UIView - 加载视图时如何收到通知?

发布于 2024-10-08 08:25:10 字数 159 浏览 3 评论 0原文

对于 UIView 来说,是否有类似于 UIViewController 的 viewDidLoad 的东西??? 我需要在 UIView 加载(UIView 的子类)后立即收到通知,并执行一些操作。

Is there anything similar to the viewDidLoad of UIViewController for a UIView???
I need to be notified as soon as a UIView has loaded (Subclass of UIView), and perform some actions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-10-15 08:25:10

根据您需要执行的操作类型,有几种技术:

  1. -(id)initWithFrame:(CGRect)frame - UIView 的指定
    初始化器;总是发送到 UIView 来初始化它,除非
    视图是从笔尖加载的;
  2. -(id)initWithCoder:(NSCoder *)coder - 每当从笔尖加载视图时,总是发送以初始化 UIView;
  3. -(void)awakeFromNib - 在nib中的所有对象初始化并连接后发送;仅当您从笔尖加载对象时才适用;你必须调用 super;
  4. -(void)willMoveToSuperview:(UIView *)newSuperview - 在视图作为子视图添加到另一个视图之前立即发送;当您从其超级视图中删除视图时,newSuperview 可能为零;
  5. -(void)willMoveToWindow:(UIWindow *)newWindow - 在视图(或其超级视图)添加到窗口之前立即发送;当您从窗口中删除视图时,newWindow 可能为零;
  6. -(void)didMoveToSuperview - 在视图插入视图层次结构后立即发送;
  7. -(void)didMoveToWindow - 在视图获取其窗口属性设置后立即发送。 -

基本上,您可以选择在初始化期间(1 和 2)、从笔尖加载之后(3)、插入视图层次结构之前(4 和 5)以及之后(6 和 7)执行操作。

Depending on what kind of actions you need to perform, there are several techniques:

  1. -(id)initWithFrame:(CGRect)frame - UIView's designated
    initializer; always sent to a UIView to initialize it, unless the
    view is loaded from a nib;
  2. -(id)initWithCoder:(NSCoder *)coder - always sent to initialize a UIView whenever the view is loaded from a nib;
  3. -(void)awakeFromNib - sent after all the objects in the nib are initialized and connected; applicable only if you load the object from a nib; you must call super;
  4. -(void)willMoveToSuperview:(UIView *)newSuperview - sent immediately before the view is added as a subview to another view; newSuperview may be nil when you remove the view from its superview;
  5. -(void)willMoveToWindow:(UIWindow *)newWindow - sent immediately before the view (or its superview) is added to a window; newWindow may be nil when you remove the view from a window;
  6. -(void)didMoveToSuperview - sent immediately after the view is inserted into a view hierarchy;
  7. -(void)didMoveToWindow - sent immediately after the view gets its window property set. -

Basically, you can choose to perform your actions during initialization (1 & 2), after loading from a nib (3), before insertion into a view hierarchy (4 & 5) and after that (6 & 7).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文