Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Saturday, June 27, 2015

Using NSNotificationCenter in Swift

NSNotificationCenter is a convenient method to implement Observer pattern in iOS, something similar to what we say Signal/Slot in Qt framework.

To register one self as observer to some event, you can use addObserver method form NSNotificationCenter. In below code you are adding self as observer, selector is method which will be called when event is triggered, name is name of event or notification in which we are interested in, and object is object from which we want to receive event from, nil means we are ready to receive event from any object.
NSNotificationCenter.defaultCenter().addObserver(
    self, 
    selector: "gameViewClosed", 
    name: "gameViewClosed", 
    object: nil);
Now to trigger the notification, you can use postNotificationName method from NSNotificationCenter. Following code shows the same. Here we are sending notification for "gameViewClosed" event, object nil mean deliver event to all interested objects.
NSNotificationCenter.defaultCenter().postNotificationName(
    "gameViewClosed", 
    object: nil);
That's it, thanks for reading.

Wednesday, March 3, 2010

Free book on Qt C++ and design pattern


I found this free online book which provide very good overview of design pattern and is very good source of learning Qt and C++.

here is link Introduction to Design Patterns in C++ with Qt4.