Monday 9 February 2009

Delegates

Objects communication is generally thought of as one way. Object A will create Object B. Then Object A can talk to Object B by calling its methods. Delegates are a way the Object B can talk back to Object A. 
This is useful in asynchronous or event driven situations. For example, Object A will tell Object B to go off and do something. Object A will then get on with its life without waiting for B to finish. When Object B is finished, it will call a delegate which will tell Object A that it is finished.

Heres how it works in Objective C.

Object B will need to keep a reference to Object A. In Object B's header wack in the following :

@interface ObjectB : NSObject 

{

    id delegate;

}


- (id)delegate;

- (void)setDelegate:(id)newDelegate;



Then chuck this in the implementation.

- (id)delegate 

{

    return delegate;

}


- (void)setDelegate:(id)newDelegate 

{

    delegate = newDelegate;

}


These are the getter and setter functions that Object A call to say it is interested in hearing from Object B. It would do something like :


    objB.delegate = self;


We need to define the function that will be called when Object B wants to talk to Object A. So back to the header file of Object B. Chuck this at the end :

@interface NSObject (ShareDelegate)

    - (void)finishedLoading:(id)sender;

@end

  
This is defining a function that will take one parameter, an id to Object B. You can have whatever parameters you want... Then when Object B wants to call the function it does this :

    if ( [delegate respondsToSelector:@selector(finishedLoading:)] ) 

    {

        [ delegate finishedLoading:self];

    }


Then all Object A has to do is define a function as normal like :

    - (void)finishedLoading:(id)sender;

    {

        self.label.text = @"done";

    }


Yippeee. Quite simple really...

Sunday 8 February 2009

What's with the square brackets (calling methods)

If you look at some objective C source code, you will see lots of square brackets around. Stuff like :

occultImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"glyph%d.gif", occult]];


seems pretty common.

To a C# programmer that just looks weird... and weird it is. 

But it basically is just another way of calling methods. When you see some square brackets, you know you are calling a method on a particular class or object. Just take it as [object method:parameter].

In the above example we are calling a static method called imageNamed on the UIImage class. The parameter we are passing is the result of another call, stringWithFormat on NSString.

In C#, this would be something like :


occultImage.image = UIImage.imageNamed(NSString.stringWithFormat(@"glyph%d.gif", occult));


What gets more confusing is when you call a method with more than one parameter. The second parameter has to be named. Like so :

NSCalendarDate *startDate = [endDate dateByAddingYears:0 months:0 days:-60 hours:0 minutes:0 seconds:0];


This line will call the function dateByAddingYears on the object endDate (which is another NSCalendarDate) object. Now the first parameter to the function is years. But we dont specify this, we just chuck in the value, which is 0 here. The second parameter is months. We have to specify the name of this parameter. and the same with days, hours, minutes and seconds.

So it is [object method:param1 param2name:param2 param3name:param3] 

Clearer now?

Oh yeah, in object C you aren't calling methods on objects, you are passing messages to them.

hmm...



Saturday 7 February 2009

Connecting up a label

Right to connect a label to a variable that we can access in code perform the following :

Chuck the following variable into your view controller .h file. See how you have to add it in two places, once as the variable within the brackets, and once as a property afterwards. The IBOutlet keyword is what makes this show up in interface builder so we can link it to the variable. Obviously you should call the label something more useful than just label which I've done here...

@interface MainViewController : UIViewController {

UILabel *label;

}


@property (nonatomic, retain) IBOutlet UILabel *label;


Then double click on the view.xib file to go into interface builder. Add the label to the View in interface builder. Then right click on the File's Owner icon and find the label underneath Outlets. To the right of this there is a little circle. If you click on it a + symbol appears. Drag this onto the label in the screen. The two should then be linked up nicely.

To change the text of the label in code just do something like the following :

label.text = @"Groove on people. Let love rule!";


And you're good to groove!

iPhone developer attaching events to buttons

Wahay all!

Right I have recently started developing for the iPhone and although the tools seem pretty robust and easy to use, working out how to start using them is a nightmare since the documentation seems to be seriously lacking. Just trying to do basic things like attaching an event to a button takes ages as I have to spend hours wading through the documentation to find the correct method parameters and all...

So here we go... I shall lay down my discoveries here as a reference for how to do the basic stuff, hopefully I can save people and myself some hassle in future.

So without further ado, lets rock and roll!!

To attach an event to a button.

Assuming you already have a .xib file with the button on it, and an associated view controller.

Declare the function in the view controller header file as (in italics)

@interface MainViewController : UIViewController {

    NSMutableData *receivedData;

}



- (IBAction)startScan:(id)sender;


I've called my event handler startScan. You should call it something else, depending on what your event will do. Mine started a scan...

The IBAction bit identifies this function as one that will show up in interface builder so we can attach this method to the button.

Then define the method in the .m file as


- (IBAction)startScan:(id)sender 

{

}


Now get funky.

Double click on the view.xib file to open up the interface in interface builder. Add your button onto the Main View. Right click on the File's Owner icon. You should see your defined method under Received Actions. To the right of the method name there is a little circle. If you click on this a + symbol appears in the circle. Drag this onto the button. Up will pop a list of possible events to attach to the method. Normally you would choose "Touch Up Inside". Wahay! 

Now all you gotta do is write some code to do something groovy inside your event handler and you are away!

Go forth and groove and let your hearts be your guides.