Saturday, December 24, 2011

Objective-C id Data Type

Class Warfare
The point of this task is getting to know the Objective-C anonymous data type, id, and to learn how to control it. This task is a view-based application which initially has a single button called “Random View”. When the button is pressed, a random anonymous object is created and based on the type of the object, the application operates as follows:
  • If the object is a UIView class or subclass thereof, it is added as a subview to the main view.
  • If the object has a background colour property, it is given a random colour before being added to the main view.
Image 1, illustrates screenshots of the application in different states. Note that objects which are recognized as UILabel or UIButton, have their class names as either their text or title properties respectively.

Figure 1: Random Object Application


The point here is taking advantage of , isKindOfClass or isMemberOfClass to determine the dynamic type of the object, and respondsToSelector to see if it has a background colour property or not. The code snippet below shows the IBAction method controlling the “Random View” button.


No comments:

Post a Comment