Category Archives: iOS

NSNotification addObserver object differentiation

In your init

what to do when the message is sent

Sending the message

When adding observers to receive notifications for a certain unique id, we can choose which object to receive it from. In our example below, we have method sexGroupUpdated: which will be executed once the notification is received for unique id SELECTED_RADIO_BUTTON_CHANGED. But we only will do this IFF those notifications are sent from self.sexGroup object.

Extending our example, let’s say in our MainView.m, we have multiple objects of the same class TNRadioButtonGroup created. Those classes all send SELECTED_RADIO_BUTTON_CHANGED unique id notifications. We can differentiate which methods the notifications will hit by putting the name of the objects that are sending those notifications:

Sending of those notifications as shown TNRadioButtonGroup.m:

It posts notification for constant string SELECTED_RADIO_BUTTON_CHANGED.

Let’s analyze in detail.

In our MainView.m, we’re adding an observer for notifications for string id SELECTED_RADIO_BUTTON_CHANGED.

But we’re getting it from three different instantiations of TNRadioButtonGroup:
sexGroup, hobbiesGroup, and temperatureGroup. They all send a notifications of id SELECTED_RADIO_BUTTON_CHANGED.

In other words, sexGroup, hobbiesGroup, and temperatureGroup all send those notifications. How do we differentiate them?

That’s where the paramter object comes in. As you noticed, we specified the object variable in which we receive these notifications:

It simply means we will execute temperatureGroupUpdated: when we receive SELECTED_RADIO_BUTTON_CHANGED notifications, but only from the object self.temperatureGroup.

UIKeyboardTypeDecimalPad

PopUpViewcontroller use retained property variable

when using PopUpViewcontroller, make sure the variable you assign to it has property strong so that it doesn’t get released.

Else, you’ll get a EXE_ACCESS error.

Notice below of self.singleColPickerPopup:

Change from NSDate to NSString

Custom UITableViewCell

JSCustomCell.h

JSCustomCell.m

result:

ViewController.h

ViewController.m

Change animation style of UIViewController

reference: http://stackoverflow.com/questions/1406037/custom-animation-for-pushing-a-uiviewcontroller

method 1

In the parent view controller, you have a button that brings in the a UIViewController. In your button responder, you go:

In the UIViewController:

Method 2

I use the following function (added to UINavigationController) to customize the push animation:

ns copying

ref: http://stackoverflow.com/questions/4089238/implementing-nscopying

To implement NSCopying, your object must respond to the -copyWithZone: selector. Here’s how you declare that you conform to it:

Then, in your object’s implementation (your .m file):

What should your code do? First, create a new instance of the object—you can call [[[self class] alloc] init] to get an initialized obejct of the current class, which works well for subclassing. Then, for any instance variables that are a subclass of NSObject that supports copying, you can call [thatObject copyWithZone:zone] for the new object. For primitive types (int, char, BOOL and friends) just set the variables to be equal. So, for your obejct Vendor, it’d look like this:

NSCopying in Base and Sub hierarchy

http://stackoverflow.com/questions/4472904/implementing-nscopying-in-subclass-of-subclass?rq=1
http://stackoverflow.com/questions/18673296/overriding-readonly-property-in-subclass

Base Class

The base is class is straightforward. We only have two member variables, but do not want others to access them. So we declare readonly in @property in the .h file. However, we DO want to modify them in our own implementation. We declare readwrite in our @property in the .m file:

Today.h

Notice the @protected above the member variables. I did this so that children classes can access these member variables.

Today.m

Be sure to synthesize them. Its basically getter/setter = iVar

In our case, since we we did not specify an iVar like this:

dayPerformance_PlacingCount is the iVar
and

makes it run through the setter/getter methods.

So the idea behind copyWithZone is that we need to make a deep copy. Hence everything should have its own address. Staying true to that assertion, we must first dynamically allocate the object to be returned:

We create an empty self class. But wait, What about its member variables? They need to be unique with their own address. So we dynamically create those member variables like so:

Then we have our newly created object’s member variables retain them. the variable copy is our newly created Day variable:

..and then we just return that copy variable.

thus, it gets returned like so:

Thus, yourDay takes on a whole newly allocated Day instance with its own member variables and all.

Base class’s copy method

To use it in your main, you go:

Result:
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 58] – ~~~~~~~~dayOne~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 59] – dayOne’s address: 0x16dc1f80
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 60] – dayPerformance_PlacingAmount address: 0x16dc1fa0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 61] – dayPerformance_PlacingAmount value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 62] – ~~~~~~~~~~~~~~~~~~~~~

-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 66] – ~~~~~~~~dayTwo~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 67] – dayTwo’s address: 0x16dc22a0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 68] – dayPerformance_PlacingAmount’s address: 0x16dc22e0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 69] – dayPerformance_PlacingAmount’s value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 70] – ~~~~~~~~~~~~~~~~~~~~~

As you can see, the Day addresses are different from the copy. As well as the member variables. In this particular case, I only printed out instance variable dayPerformance_PlacingAmount.

Fast Enumeration with Collections

Fast enumeration is an Objective-C’s feature that helps in enumerating through a collection.

Collections in Objective-C

Collections are fundamental constructs. It is used to hold and manage other objects. The whole purpose of a collection is that it provides a common way to store and retrieve objects efficiently.

There are several different types of collections. While they all fulfil the same purpose of being able to hold other objects, they differ mostly in the way objects are retrieved. The most common collections used in Objective-C are:

NSSet

NSArray

NSDictionary

NSMutableSet

NSMutableArray

NSMutableDictionary

Fast enumeration Syntax

for (classType variable in collectionObject )
{
statements
}

Here is an example for fast enumeration.