Part 2: Nappies and training wheels – A .net developer learns objective-c for iPhone iPad

Just going through some early podcast videos from the Cocoa Touch Netcast (search cocoatouch netcast in the itunes store).

The first video is good going off the back of the videos mentioned in my last post. The 2nd video ep 2: Slider is also helpful. It clearly explains Class inheritance (a class is your code, classes can be inherited from super classes bringing with it certain functionality), actions and outlets (the way which the UI interacts back and forth with the code) and a good demo of a basic slider control flinging left to right adjusting with it the text in a label of the sliders current position. A good hello world (without the text hello world).

Have a good pause at 11mins into it for a good screen explaining actions and outlets.

outlet -> points to label
action -> references a slider’s event

So the code in SliderViewController.h would look something like:

#import <UIKit/UIKit.h>
@interface SliderViewController : UIViewController {
UILabel *sliderLabel;
@property (nonatomic, retain) IBOutlet UILabel *sliderLabel;
- (IBAction)sliderChanged(id)sender;

And with any .h file there is a matching .m file, SliderViewController.m would look like this:

#import "SliderViewController.h"
@implementation SliderViewController
@synthesize sliderLabel;
-(IBAction)SliderChanged(id)sender {
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:@"%d", progressAsInt];
sliderLabel.text = newText;
[newText release];
-(void)dealloc {
[sliderLabel release];
[super dealloc];

Armed with limited knowledge (feel free to correct me) I see variables and methods are referenced in the .h file (variables within the {}’s and methods after). Naming a variable (ie sliderLabel) the same as the outlet seems to give you the ability to speak directly with the label on the UI.

You need to call @synthesize to be able to use the sliderLabel variable on the .m file. This I assume from the example is to be called for every variable mentioned in the .h file.

So … time for me to do some damage. Stay tuned, lets see if I can remember to alloc, init and release!


Leave a Reply

Your email address will not be published. Required fields are marked *