【 ios】监视卷键,导致多个调用

发布于 2025-02-04 09:25:55 字数 750 浏览 0 评论 0原文

我想通过单击“音量”按钮来进行一些操作。我可以通过以下方式做到这一点

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

 - (void)volumeChangeNotification:(NSNotification *)notification {
  //do something
 }

,但我发现了一个令人困惑的现象。单击卷键一次,监视事件将被调用两次。我尝试在侦听器函数末尾删除侦听器,这样

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

- (void)volumeChangeNotification:(NSNotification *)notification {
//do something
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SystemVolumeDidChange" object:nil];
}

,我在1秒钟后打开监视器,但这也行不通。我该怎么办?非常感谢

I want to do some operations by clicking the volume button. I can do this in the following ways

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

 - (void)volumeChangeNotification:(NSNotification *)notification {
  //do something
 }

But I found a puzzling phenomenon. Click the volume key once, and the monitoring event will be called twice. I try to delete the listener at the end of the listener function ,like this

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

- (void)volumeChangeNotification:(NSNotification *)notification {
//do something
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SystemVolumeDidChange" object:nil];
}

Then I turn on the monitor after 1 second, but it also doesn't work. What should I do?thanks verymuch

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文