Ok, this may seem like a dupe question, but if you bear with me and read the whole question, you see why I ask it.
I have seen many nice examples and explanations on the web and here as well, but I just cant get it. Its important for me to understand the whys and the 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 abundance of examples) because then I just don't understand it.
I would like two classes, a gardenClass. It has an array with flowers, each flower is instantiated from the flowerClass. So, the flowerClass does not know what kind of garden instantiated it. It just screams for water sometimes.
So, if someone here could explain this for me, I would be really grateful!
I tried out and here is me having a go based on the info I got 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 have not imported any UIKit or foundation, beause I tried to just do the stuff thats related to delegates here now... So, is this correct?