Can someone explain very simply how Objective-C delegates work?

Well, this may seem like a hoax, but if you carry with me and read the whole question, you will understand why I ask about it.

I have seen many good examples and explanations on the Internet and here, but I just cannot get it. It is important for me to understand whys and hows, so instead of using some example from the documentation (BTW, I really suck when it comes to understanding Apple Docs, Im used with php.net documentation, with lots of examples ), because then I just do not understand.

I would like two classes - a garden. It has an array with flowers, each flower is created from flowerClass. Thus, flowerClass does not know which garden it created. He just screams about water sometimes.

So, if someone here can explain this to me, I would be very grateful!

I tried, and now I'm going down based on the information I received from you guys:

MyGarden.h

#import "Flower.h"

@interface MyGarden : NSObject <WateringDelegate>
{
    Flower *pinkFlower;
}
@end

MyGarden.m

#import "MyGarden.h"
#import "Flower.h"

@implementation MyGarden

- (void) giveWaterToFlower
{
    NSLog(@"Watering Flower");
}

- (void)viewDidLoad
{
    pinkFlower = [[Flower alloc] init];
    [pinkFlower setDelegate:self];
    [pinkFlower startToGrow];
}
@end

Flower.h

@protocol WateringDelegate <NSObject>
@required
- (void) giveWaterToFlower;
@end

@interface Flower : NSObject
{
    id <WateringDelegate> delegate;
}

@property (retain) id delegate;
- (void) startToGrow;
@end

Flower.m

#import "Flower.h"

@implementation Flower

@synthesize delegate;

- (void) needWater
{
    [[self delegate] giveWaterToFlower];
}

- (void) startToGrow
{
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(needWater) userInfo:nil repeats:YES];
}
@end

I did not import any UIKit or foundations because I was trying to just do the delegate related stuff here now ... So, is that right?

+3
source share
3 answers

A delegate is where you implement custom behavior for your application that affects how its other class works (for example, a regular object in a Cocoa structure).

, , , . Objective-C , , , , .

, , , , , .. , , , , . , , , , , .. , ( ), , . , .

, , . , .

, - "waterMe". (, - ) , , , . , , , , . , . , , , .

+4

, . , Apple , . , Apple ( , imo) .

, . : ( ) , (.. , , ).

, , , . #, , += , . Objective-C, , , , .

, . , , . (, , ) .

, Apple , tableView:didSelectRowAtIndexPath. . , ? "" ( ) , - . .

, Apple ( ), tableView:cellForRowAtIndexPath. tableView , .

, , . , , , .

() () , ( , ). - , . , Objective-C, , , .

0

, , self.delegate giveWaterToFlower. delegate MyGarden, giveWaterToFlower MyGarden , . wateringCan, wateringCan [pinkFlower setDelegate:self]; , , self.delegate giveWaterToFlower giveWaterToFlower wateringCan, , . , gardenHose, giveWaterToFlower. , .

But the design pattern is that it flowerdelegated it to a method on giveWaterToFloweranother object. The object flowerdoes not know who provides it giveWaterToFlower. You can enable the giveWaterToFlowerdefault method in flowerand initialize the objects flowerwithself.delegate = self;

0
source

All Articles