Solved

need help understanding delegates and delegation in iOS

Posted on 2013-01-19
2
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

I recently went through the process of creating an installable image of an iPhone mobile app (an .ipa file) that beta testers could load onto their test devices. The process involved quite a few steps, but both Appcelerator and Diawi.org had decent …
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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