Create a Notification Center like in Objective-C for CoronaSDK

If you have used Objective-C you might have come across Notification Center, at first this is a strange concept, as it is not used in many languages.
However when you use it a couple of times, you realise that it is very useful. Then there are delegates and for most developers there are events. Now each have their pros and cons. Let us look at them and what they do.

Events are the simplest and most important ones for GUI based development. So what should happen when the user taps on an object, what should happen when the data in a switch changes from ON to OFF. So events are used to notify the user to be able to handle it at that point.

Delegates, are used in Objective-C (I hate Java with a passion, but am told that delegates are more like abstract classes, dunno if that is correct) and the point being that you create a two way reference from object_A to object_B and call a function from Object_A while processing something in Object_B. So this could be like events but for user defined objects that register themselves and have a function with a particular signature to be called. The function is always fixed and this will respond to only one delegate per object.

Notification, this is the beauty in Objective-C, which I guess Microsoft used to call Message Queues. The difference being that Message Queues are global which means your application can Peek at the messages for Word or Excel without having any connection with the same. Where as Notification Centers are Application Wide and allow various objects to be notified of the messages.

Hope this was a brief explanation of what these three are, in CoronaSDK, there are times when you might want to have functionality like the framework provided ones, e.g. onComplete, onStart, etc

Now you can, I have created a non-visual custom Control that allows for Notifications to be used. The usage is simple

   local notif = require("Notification")
   local notify = notif.new()
   
   notify.addHook("Test",function() print("Hello World!") end)

The hook is created and set, it can be called by invoking the notify method

   notify.Notify("Test")

when done, and you want to remove a hook, it is also as simple as
   notify.removeHook("Test")

instead of the inline function, you can also add hooks like
   local function best()
    print ("This is a function called from the notification")
   end

  notify.addHook("Best",best)

cheers,

?:)

Comments

Popular Posts