Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Using NSNotificationCenter help
-
I'm looking at how to use NSNotificationCenter (which is needed for things like external gamecontrollers) and I have no idea how to make it work. I tried quite a bit but now I'm stuck with this little code:
from objc_util import * Notification_Center = ObjCClass('NSNotificationCenter').defaultCenter() def test(arg): print 'Received notification!' class test_c(): pass Notification_Center.addObserver_selector_name_object_(test_c,test,'moetest',None) Notification_Center.postNotificationName_object_('moetest',self)
It throws a wrong argument type exception.
-
You need to create an actual Objective-C class to receive notifications. The latest beta (#160023) makes this a bit easier with the
create_objc_class
function. Here's a simple example:from objc_util import * center = ObjCClass('NSNotificationCenter').defaultCenter() def test_(_self, _cmd, n): # note: the trailing underscore in the method name is important. notification = ObjCInstance(n) print 'Received notification: %s' % notification MoeObserver = create_objc_class('MoeObserver', methods=[test_]) observer = MoeObserver.alloc().init() center.addObserver_selector_name_object_(observer, 'test:', 'moetest', None) center.postNotificationName_object_('moetest', None)
-
Thank you very much! Worked just fine (except for the string formatting in test_, for whatever reason) :D
-
Is the Notification module not appropriate for the current project?
-
NSNotificationCenter isnt not related at all to the UI-Notification-Center. NSNotificationCenter is some kind of async messaging system. You can register a function to be called when a certain message is sent in your program, somewhat similar to a delegate.
-
Thanks for the info.