Solved

need help understanding delegates and delegation in iOS

Posted on 2013-01-19
2
471 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now