Oreilly Learning iPhone Programming

Currently on Ch5 but have a few questions looming in the back of my mind:

1) Why use a tableviewcell to enter data for AddNewCity instead of a UIView with label and description?

2) Just as i thought, tag 777 got me in trouble. ¬†My bad because i added the tag to both the inner component uitextField and the outer component tableViewCell by mistake. ¬†But im wondering ¬†a couple of things: (a) can i tag the entire structure uitableViewCell as i did (without tagging the inner components) and get any component inside it with that tag refrence? (b) why exactly was it not passing the data correctly; because the outer component and inner component had the same tag…because other components can indeed have the same tag, like the same uitextfield and uitextview. (ive gotta try this out)

3)device.batteryMonitoringEnabled = appDelegate.monitorBattery;

which seems it should be the other way around.  Why does my mind do this to me?

4) just realized something…self.var = NO; is fundamentally diffnt from var = NO; in that in the former you are sending var the message of NO, using its accessor methods which only exist if you have synthesized the ivar as a property! ¬†Whereas in the latter you are simply setting a variable to the value NO!

5) @class allows a forward reference without loading the entire file, apparently just the ivars, properties and methods. But #import still required?

6) cool convenience methods like; levelLabel.text = [self batteryLevel]; to set a variable

7)Edit button is for tableView and calls setEditing method. Add button needs a specific method defined by user to be called.

Delegates & Protocols iOS

A complex subject to wrap your head around, i agree!  But its pretty cool once you get a working example going.

Let’s think of a game where a lander comes down from the sky (presumably on Mars – since Mars is a hot topic lately) and deploys a ¬†rover. ¬†In Cocos2d, you put all your objects on a Layer object. ¬†This means the layer class is the main class (analogous to a viewcontroller class contains buttons, views, labels, cells etc).

You create a Lander object class because you want to re-use it everywhere in the game. ¬†With every new level, you must create a new layer class but you can reuse your Lander Class. ¬†So you wan’t your Lander to be able to deploy these rovers so that each time you reuse the lander, it can deploy rovers on any given layer.

Let’s look at this image:

Marcio Valenzuela at Santiapps.com
Marcio Valenzuela at Santiapps.com

The Lander object declares a GameplayLayerDelegate property.  This means any lander object will be a delegate to the GameplayLayer.  This means the Lander can call the mystery protocol method in order to create the Rover object by saying:

[delegate mysteryMethod];

And the GameplayLayer only has to adopt the GameplayLayerDelegate.

In the case of an iOS app, a typical scenario might be a To Do List where you have a list of items and you wish to add a new item. ¬†You have 2 view controllers: one lists the existing items and the other is called when the user taps on the “Add” button and wants to add a new item. ¬†You might have something that looks like this:

Delegates & Protocols: Santiapps.com by Marcio Valenzuela
Delegates & Protocols: Santiapps.com by Marcio Valenzuela

In this scenario the AddItemViewController on the right will want to take the user input and report it back to the ViewController on the left labeled To Do List.  The steps are:

1)  The ViewController on the left will conform to or adopt the AddItemViewControllerDelegate.

2) The AddItemViewController class declares its protocol like so:

@protocol AddItemViewControllerDelegate <NSObject>

– (void)addItemViewControllerDidCancel:(AddItemViewController *)controller;

– (void)addItemViewController:(AddItemViewController *)controller didFinishAddingItem:(ToDoItem *)item;

– (void)addItemViewController:(AddItemViewController *)controller didFinishEditingItem:(ToDoItem *)item;


3) And it declares the property:

@property (nonatomic, weak) id <AddItemViewControllerDelegate> delegate;

4) Finally the AddItemViewController calls those mystery methods by saying:

– (IBAction)cancel{

[self.delegate addItemViewControllerDidCancel:self];


– (IBAction)done{

// Do whatever in order to capture user data

//Now send that info back to the delegate

[self.delegate addItemViewController:self didFinishAddingItem:item];