Solved

need help understanding delegates and delegation in iOS

Posted on 2013-01-19
2
477 Views
Last Modified: 2013-01-20
I have been reading through the Big Nerd Ranch book and in chapter 4, on delegation, I have gone through and read it and done the coding but I still don't understand what delegates are.

Can someone help me understand delegation and its purpose and why I need to know it? What are the consequences of not knowing it? Thanks
0
Comment
Question by:Mark_Co
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 38798397
Who is a delegate in the real life?
One man acts in behalf of someone else.
Same in Objective-C.
One object wants another object to perform a specific task - this second object is the delegate, the description of the task is the protocol.

In Objective-C, we:
1. define a protocol (keyword @protocol)
2. add id <my_protocol> delegate  property to a class
3. instantiate an object of this class
4. implement the protocol in another object (the method are declared in the protocol and implemented in the delegate).
5. assign this last object to be the delegate in the first object.
Now from the first object we can call the methods defined in the protocol.

Here is a very short example that just illustrates the pattern:
#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>
- (void)print:(NSInteger)number;
@end

@interface MyClass : NSObject

@property (assign) id <MyProtocol> delegate;

- (void)doSomething;

@end

@implementation MyClass

- (void)doSomething
{
    NSInteger x = 10;
    if ( [self delegate] ) {
        [[self delegate] print:x];
    }
}

@end

@interface MyDelegate : NSObject <MyProtocol>

@end

@implementation MyDelegate

- (void)print:(NSInteger)number
{
    NSLog(@"number = %li", number);
}

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        MyClass *first = [[MyClass alloc] init];
        MyDelegate *second = [[MyDelegate alloc] init];
        [first setDelegate:second];
        
        [first doSomething];
        
        [second release];
        [first release];
    }
    return 0;
}

Open in new window


In Xcode, you can create new project from OS X Command Line Tool (Foundation) template and replace the code in main.m file. The output is following:
2013-01-20 15:33:28.113 TestDelegate[2620:303] number = 10
0
 

Author Closing Comment

by:Mark_Co
ID: 38799531
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
There is a security feature on iOS devices that is nearly impenetrable when it has been activated.  This article will provide some possible solutions as well as necessary steps to take to ensure you do not end up with a locked device.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question