最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - All Window Controllers respond to didBecomeKeyNotification - Stack Overflow

programmeradmin2浏览0评论

I'm very new to the macOS/AppKit world, so please be patient if I don't adequately understand many macOS/AppKit concepts. Over the centuries (since the Ford administration), I've coded in many different environments, including several stints in operating system development, so programming isn't new to me.

I'm writing my first Swift app for my Mac. As a single-window app, I figured out what needed to be programed and it worked. Then, I decided to stretch it to be a multi-window app which worked save for a few new wrinkles.

One wrinkle is that I pass notifications from the app delegate to the view controllers. I've added observers in the view controller and send notifications. The problem is that every view controller receives the notification and attempts to process it, which may cause the program to crash, depending on it's state.

What I did to prevent this was to add a dictionary element (along with the other elements) containing the address of the view controller structure ("vcID" - view controller ID), which the receiving view controller examines to determine if the address is the same as the address of its own view controller. If it's not, the it just returns. If it is, it continues processing.

Simple, but obviously it's not efficient to wake up a bunch of threads only to have all but one just go back to sleep.

So, my question is: how do I notify a specific view controller in a Swift-flavored way to wake up and process the request?

Similarly, I want to know which view controller has just been given the focus as the mouse clicks in each window of the app. I've added an observer to the view controller to watch for the didBecomeKeyNotification, assuming that just one view controller would receive the notification. But again, as above, all view controllers wake up, not just the one that became key.

I suspect that both situations are caused by the same problem.

I'm very new to the macOS/AppKit world, so please be patient if I don't adequately understand many macOS/AppKit concepts. Over the centuries (since the Ford administration), I've coded in many different environments, including several stints in operating system development, so programming isn't new to me.

I'm writing my first Swift app for my Mac. As a single-window app, I figured out what needed to be programed and it worked. Then, I decided to stretch it to be a multi-window app which worked save for a few new wrinkles.

One wrinkle is that I pass notifications from the app delegate to the view controllers. I've added observers in the view controller and send notifications. The problem is that every view controller receives the notification and attempts to process it, which may cause the program to crash, depending on it's state.

What I did to prevent this was to add a dictionary element (along with the other elements) containing the address of the view controller structure ("vcID" - view controller ID), which the receiving view controller examines to determine if the address is the same as the address of its own view controller. If it's not, the it just returns. If it is, it continues processing.

Simple, but obviously it's not efficient to wake up a bunch of threads only to have all but one just go back to sleep.

So, my question is: how do I notify a specific view controller in a Swift-flavored way to wake up and process the request?

Similarly, I want to know which view controller has just been given the focus as the mouse clicks in each window of the app. I've added an observer to the view controller to watch for the didBecomeKeyNotification, assuming that just one view controller would receive the notification. But again, as above, all view controllers wake up, not just the one that became key.

I suspect that both situations are caused by the same problem.

Share Improve this question edited Mar 31 at 16:50 HangarRash 15.1k5 gold badges20 silver badges55 bronze badges asked Mar 31 at 8:24 user29306905user29306905 6
  • To send a message to a specific view controller, call a method. – Willeke Commented Mar 31 at 10:56
  • The other issue should be asked in another question with a minimal reproducible example. – Willeke Commented Mar 31 at 10:59
  • In re "other issue": I found the problem for didBecomeKeyNotification sent to all windows - I changed object: nil to object: window in the call to NotificationCenter.default.addObserver ( ). Now the notification is only sent to the one app window that has the focus. I tripped over the solution in another stack overflow question. – user29306905 Commented Apr 1 at 1:48
  • Please edit the title and the question to remove the "other issue". – Willeke Commented Apr 1 at 4:15
  • As noted when I originally asked the question, I suspected that both situations had the same underlying problem because the symptom was identical. This turned out to be correct. When I applied the same fix in the other situation, the problem stopped there, as well. This being the case, I'm going to politely decline the suggestions to ask a separation question and edit the title. I will take the suggestions under advisement along with providing a minimal reproducible example for the next question I submit. – user29306905 Commented Apr 1 at 21:04
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Credit for the complete explanation/answer to this question was posted back in 2012 by @Thilo in the context of a different question:

That "object" parameter to "addObserver" is an optional filter. Upon posting a notification you can set an object to the sender of the notification, and will then only be notified of that sender's events. If set to "nil" you will get all notification of this type (regardless who sent them).

My learning continues.

发布评论

评论列表(0)

  1. 暂无评论