检测 UIView 修改

发布于 2024-10-22 03:36:28 字数 152 浏览 2 评论 0原文

我需要从应用程序委托中检测到主 UIView(用户实际正在观看的视图)已更改。

  1. 他有 iOS 通知吗?
  2. 如果没有,我可以从计时器执行一个函数...但是如何检查主视图(用户实际正在观看的视图)是否已更改?

-弗雷德

I need to detect from the application delegate that the main UIView(the view that the user is actually watching) has changed.

  1. His there an iOS notification for that ?
  2. If not, i can execute a function from a timer...but how to check that the main view (the view that the user is actually watching) has changed ?

-Fred

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

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

发布评论

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

评论(3

唐婉 2024-10-29 03:36:28

您应该调整 UIViewController 类的选择器。就像您可以将 viewWillAppear 混合到您创建的 customViewWillAppear 中一样。因此,当应用程序生成对 viewWillAppear 的调用时,您的 swizzled 方法(customViewWillAppear)将被调用。因此,您可以识别出用户已更改屏幕。

不要忘记调用实际方法来执行 viewWillAppear 方法中编写的内容。

You should swizzle your selector for UIViewController class. Like you can swizzle for viewWillAppear to your created customViewWillAppear. So, when application will generate call for viewWillAppear, your swizzled method(customViewWillAppear) will get called. So, you can identify that, user has changed the screen.

Do not forget to call actual method to execute the stuff which are written in viewWillAppear method.

夜灵血窟げ 2024-10-29 03:36:28

如果您希望它是通用的,您可以尝试使用通知。

在任何 UIView 中,您想要发出其更改“消失”的信号,您可以在 dealloc 方法中添加通知

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewChanged" object:nil];
}

,并且在响应更改的代码中,您可以侦听“ViewChanged”通知。您可以使用以下代码执行此操作:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewChanged) name:@"ViewChanged" object:nil];

-(void)viewChanged{
 // do what ever you want after the view has changed
}

If you want it to be generic you can try using notifications.

In any UIView you want to signal its change "disappearance" you can add your notification in the dealloc method

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewChanged" object:nil];
}

and in the code that is responding to the change you can listen for the "ViewChanged" notification. You can do this with this code:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewChanged) name:@"ViewChanged" object:nil];

-(void)viewChanged{
 // do what ever you want after the view has changed
}
○愚か者の日 2024-10-29 03:36:28

视图可以更改的唯一方法是您的应用程序在某种程度上更改了它。

The only way a view can change is if your app changes it at some level.

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