Skip to main content

Using delegates in Objective-C



Why use delegation?

Well when writing Objective-C programs in Xcode we are encouraged to use the MVC(Model View Controller) structure.In MVC a model cannot send messages directly to a specific controller.This can become a problem if data in your model changes independantly and you need to update this information in your view via the controller(The model must never communicate directly with the view).This is where a delegate comes in,it allows messages to be sent from the model to a controller that is not specified in the model but is rather specified by the controller,in other words the controller specifies itself.

If you dont really understand what im talking about so far ,don't worry.I didn't understand delegates either until I needed one,but hopefully by the end of this tutorial you will understand not only how to use a delegate ,but the reason you would want to use one.

Program breakdown

For this tutorial we will write a program that has a model that will change an integer value on a timer event and we will have a view with a label and a controller that will update the value displayed on the label.We will then use delegation to tell the controller when it needs to update the label by broadcasting messages from the model.

First things first ,lets open up Xcode and create a single view based iPhone application With ARC(Automatic Reference Counting) enabled,I tried doing this without ARC before and alot of things broke and I actually wasn't able to fix it.I am also using storyboards,you can use Xibs if you know what you are doing.



Next lets add a label to the view and link it to our controller by selecting assistant editor and ctrl+dragging it into the Controller code.Make it an outlet and name it textLabel.

Assistant Editor icon:
Creating an Outlet:


This should generate the following code in ViewController.h:

@property (weaknonatomicIBOutlet UILabel *textLabel;

Now lets create our model,create a new Objective-C class That is a subclass of NSObject and name it NumberLooper or anything you deem appropraite.



Now lets start coding,in NumberLooper.h add the following code:

#import <Foundation/Foundation.h>
@interface NumberLooper : NSObject{
    NSTimer *timer;
    int currentNumber;
}
@property (nonatomic,strongNSTimer *timer;
-(void)startTimerLoop;
@end
Now lets implement the model in NumberLooper.m

#import "NumberLooper.h"
@implementation NumberLooper
@synthesize timer;
-(void)timerEvent:(NSTimer*)timer{
    /*if the number is less than 256 add 1 and else set 
     the number to 0*/
    
    if(currentNumber < 256)
        currentNumber ++;
    else
        currentNumber = 0;
    
    //Display the number in the console
    NSLog(@"%d",currentNumber);
}
-(void)startTimerLoop{
    if (!timer){
timer=[ NSTimer scheduledTimerWithTimeInterval:0.020 target:self  
selector:@selector(timerEvent:) userInfo:nil repeats:YES ];
        NSLog(@"Timer started.");
    }
    else {
        NSLog(@"Timer is already running");
    }
}
@end
This will begin a loop that will constantly change the value of currentNumber once startTimerLoop is called,however if we ran the application now we wouldnt see this output in the console because satrtTimerLoop is never called,so lets call it ,go to ViewController.h and add
#import "NumberLooper.h"
then go to ViewController.m and add the following code to - (void)viewDidLoad:

 NumberLooper *nl = [[NumberLooper allocinit];
    [nl startTimerLoop];
Now when we run the app on the simulator we can see the console output in Xcode of the number changing


As you can see the label is still not updating ,which is good because if it is then some sort of voodoo magic has occured because we haven't even written the delegate yet.I think its about time we do that.First we create a @protocol in the model in NumberLooper.h above the interface ,this lets us specify what messages are sent to the delegate:

@protocol NumberLooperDelegate <NSObject>
-(void)numberHaschangedTo:(int)number;
@end
@interface NumberLooper : NSObject{ ...
Then we set the delegate as a property in the interface,this property must always be strong.

@property (nonatomic,strongNSTimer *timer;
@property (nonatomic,strongid <NumberLooperDelegate> delegate;
-(void)startTimerLoop;
Now lets move to the implementation in NumberLooper.m and synthesize the delegate:

@synthesize delegate;
And now we send the message to the delegate in timerEvent just after we write the number to the console:

 //Display the number in the console
    NSLog(@"%d",currentNumber);
    
    //send the message to the delegate
    [self.delegate numberHaschangedTo:currentNumber];
That wraps up all the code we need to implement in the model ,now lets move to ViewController.h,here we will tell ViewController that it can be a NumberLooperDelegate by adding to the end of its subclass int the interface declaration like this:

@interface ViewController : UIViewController <NumberLooperDelegate>;
@property (weaknonatomicIBOutlet UILabel *textLabel;
@end
Now we implement the -(void)numberHasChangedTo: method that we declared in our @protocol in the @implementation of ViewController in ViewController.m:

#pragma mark - NumberLooperDelegate -
-(void)numberHaschangedTo:(int)number{
    //set the textlabel text value to the number
    textLabel.text = [NSString stringWithFormat:@"%d",number];
}
The last thing that is left for us to do is to set ViewController as NumberLooper's delegate so that it knows who to send its message to ,we do this just before we call startLoopTimer using [NumberLooper Instance Name].delegate = self:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NumberLooper *nl = [[NumberLooper allocinit];
    nl.delegate = self;
    [nl startTimerLoop];
    
}
Now when we run the application the label should update every time the value of currentNumber changes:



I hope you now have a good understanding of how to use delegates and why you would use them so that you can now go and try them out in your own apps.

If your app did not compile or didn't work properly or you have any questiions or some part of the tutorial was unclear to you and you need help,feel free to comment,i will reply to questions when I have time.

Comments

  1. Great tutorial. I have been struggling to grasp the concept.

    ReplyDelete
  2. I tried to use this in my app. But it is not working. Do you mind looking at my code?

    --Yuval

    ReplyDelete
    Replies
    1. You should first try the tutorial as a separate app to see if you can master the concept, after that adding it to your app should be easy.

      Delete
  3. @property (nonatomic,strong) id delegate;
    Why you're setting it as strong? It should be weak.

    ReplyDelete
    Replies
    1. Well if weak works for you thats great , but I have personally had issues where the delegate was forgotten when it was weak.

      Delete
    2. Setting weak will prevent you from retain cycles. I believe that with ARC you probably can use strong. Anyway I'm a bit old-fashioned. Please let me know about the issues with setting strong. Maybe I could explain.

      Delete
  4. Excellent tutorial. I appreciate you taking the time to write this, thank you!

    ReplyDelete
  5. Wow you just saved me a ton of headaches!!! I was trying to create a CLLocationManager Singleton and everywhere I looked made it so confusing. After reading this very simple clear explanation of how to use delegates I'm off and running.
    Thanks a million!
    RON

    ReplyDelete
  6. One of best explanation availbale on internet, thanks so much!

    ReplyDelete
  7. Agreed. Brilliant explanation.

    ReplyDelete
  8. Thank's, now I can make my own delegate.

    ReplyDelete
  9. Great tutorial! :) Now I know how delegate works. thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Setting up Qt Creator for assembly

After fiddling with inline asssembly (not very successfully) ,I recently decided to try writing proper assembly and compiling with NASM in Linux. After writing a hello world using gedit and having a terminal open for compiling,linking and running I had a thought.,there has to be a better way to do this. So I tried Qt Creator ,because I know it's easy to add custom build and run commands,and what do you know? I got it to work. Here's how,my screenshots and assembly code are in Linux but the set up should be the same regardless of the operating system,if you are not using Linux then just use the same commands you use to assemble in your operating system. First off ,create a new console application: I named mine ASM Rename the main.cpp to main.asm and delete all the text inside.then insert some assembly: Now open up the “Project” tab and edit the build settings,remove the current build and clean steps and remove the “-build-desktop” from the en

Fix ssh -Y with other Macs OSX 10.8 Mountain Lion

You may have realised that when you try use ssh -Y on another Mac from Mountain Lion you get the following error : Warning: No xauth data; using fake authentication data for X11 forwarding.X11 forwarding request failed on channel 0 Fixing this error only takes some simple configuration. All you have to do is add these lines to the following files on both machines: /etc/sshd_config X11Forwarding yes XauthLocation /opt/X11/bin/xauth XauthLocation /usr/X11R6/bin/xauth /etc/ssh_config XauthLocation /opt/X11/bin/xauth XauthLocation /usr/X11R6/bin/xauth That should be all you need to get rid of the authentication error. Hope this was helpful.